Hack Forums
[C] simple C mistakes to learn from - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] simple C mistakes to learn from (/showthread.php?tid=5186576)



simple C mistakes to learn from - Bulgarian - Feb 28, 2016

SolvedFirstProblem (Click to View)
solvedSecondProblem (Click to View)


Another one...


Open a text file that contains two numbers/dimensions separated by either a comma or a space, followed by a block of what appears to be a maze from character #.
There is one 's' char and one 'g' char, these are start and goal. # is the wall of the maze.
If there is no such file exit the program. If the numbers/dimensions are less than 10, exit the program.
The first number represents the rows of the maze, the second number represents the columns of the maze.
If the dimensions pass, then check each line to see if there is any other character than 's', 'g', '#'. If any other characters are presents the program should exit. If not, display the maze contained in the text file surrounded by a frame. The top and bottom should be dashes the left and right should be | pipe chars.


This is what I've tried so far...

Quote:#include <stdio.h>

int main ()
{
FILE file;
char c;
int n = 0;
char fLine [100];
int rows = 0;
int colms = 0;


file = fopen("maze.txt", "r");

if (file==NULL){
perror ("Error reading file");
return(-1);
}
else if(fgets (fLine , 100 , file) != NULL ){

puts (fLine);
sscanf (fLine,"%i %i",&rows,&colms);

if ( rows <= 10 || colms <= 10){
printf("Dimensions are too small");
fclose (file);
return(-1);

}
else
printf("%i %i\n", rows, colms);
printf("There are %i rows \n", rows);
printf("There are %i columns \n", colms);
}


/
if (c != 's' || 'g' || '#')
printf("You have invalid character");

else
{
do {
printf("%c", c);
} while (c != EOF);
*/

}

Both numbers get allocated into the variables!

Oh and a sample file for the maze.tx is:

Quote:12, 15
####s### ####### #########
####### ##### ###### ### #
## ####### ### ###### ####
#### ########## ###### ###
# ####### ######## #### ##
#### ######g######## #####




* - Remix - Feb 28, 2016

Code
if(c=='U' || c=='u')
{
    for(c='A'; c<='Z'; ++c)
    printf("%c ",c);
} else if (c=='L' || c=='l')
{
    for(c='a'; c<='z'; ++c)
    printf("%c ",c);
} else
{
    printf("Error!!!");
}

In the condition you checked whether c is l or u


* - God of Thunder - Feb 28, 2016

Remix correctly pointed it out, it's the problem with this IF condition

[Image: l2tCPnH.png]


* - Bulgarian - Feb 28, 2016

Oh wow thank you so much! It's those small mistakes that get me thinking for hours and make me feel stupid lol I have another problem, should I keep going with the questions? they might help some people afterwards to see that you have to pay very close attention. Once again thank you guys!


* - God of Thunder - Feb 28, 2016

(Feb 28, 2016 01:31 PM)WhiteG Wrote:  Oh wow thank you so much! It's those small mistakes that get me thinking for hours and make me feel stupid lol I have another problem, should I keep going with the questions? they might help some people afterwards to see that you have to pay very close attention. Once again thank you guys!

Don't let anything stop you from coding. If you want to learn it, then do it. That's how you'll get better at it :)


* - Bulgarian - Feb 28, 2016

(Feb 28, 2016 01:35 PM)Deadpool Wrote:  Don't let anything stop you from coding. If you want to learn it, then do it. That's how you'll get better at it :)

Thank you so much. I've realized that I wasted my time on languages like html,css, js. None of that helps for Computer Scientist other than the very basic concept of computer languages. BTW I've uploaded another problem I'm facing, this is a little harder exerciser.