Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

simple C mistakes to learn from

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######## #####

[Image: nExO5H4.png]
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
Remix correctly pointed it out, it's the problem with this IF condition

[Image: l2tCPnH.png]


BRING ME THANOSSSSSS



PM me to buy this spot.
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!
[Image: nExO5H4.png]
(Feb 28, 2016 01:31 PM)Bulgarian 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 :)


BRING ME THANOSSSSSS



PM me to buy this spot.
(Feb 28, 2016 01:35 PM)God of Thunder 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.
[Image: nExO5H4.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by HackersFeb 3, 2025 08:24 AM 0 168 Feb 3, 2025 08:24 AM
Last Post: Hackers
[Python] by CyberErrorAug 6, 2018 09:24 PM 6 142 Aug 6, 2018 09:24 PM
Last Post: CyberError
[C] by Warren BuffettMar 19, 2015 10:34 PM 5 228 Mar 19, 2015 10:34 PM
Last Post: RTLZER0