Thread: Writing a beginner C program and I need someone to answer a quick Q

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Vancouver, British Columbia, Canada
    Posts
    4

    Writing a beginner C program and I need someone to answer a quick Q

    Hello, first post here, I'm writing a simple program and need some help.

    I get a fatal error whenever I run this program, and I cannot assess why.

    Should the for loop not stop once it reaches a scanf function? As I am assuming the program is crashing because it loops into infinity I think that's the problem. I would greatly appreciate it if someone could enlighten me as to whats going on.

    Also, I'm aware that it makes far more sense to use a while loop but I'm asking this question to help me in understanding the for function, not particularly how to re-write the program using other methods.

    /****************************/

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int myguess; 
    	int compguess=rand() %100;
    	int final=1;
    
    	printf("Welcome, to the number guessing game, I have chosen a number, now you must choose yours.");
    	scanf("%d",myguess);
    	
    	for(;final==0;)
    	{
    		if(myguess>compguess);
    		{
    		printf("You guessed %d, which is higher than the number I have chosen.",myguess);
    		printf("\nGuess again: ");
    		scanf("%d",myguess);
    		}
    
    		if(myguess<compguess);
    		{
    		printf("You guessed %d, which is less than the number I have chosen.",myguess);
    		printf("\nGuess again: ");
    		scanf("%d",myguess);
    		}
    
    		if(myguess==compguess);
    		{
    		printf("You guessed %d, which is the number I have chosen. Good job & goodbye.",myguess);
    		final=1;
    		}
    	}
    
    
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I am not sure what you mean by "understanding the for function" but here are some things that should get your program working. First of, scanf expects a pointer to store information to so add a & before all your variable statements. So it should be :
    Code:
    scanf("%d", &myguess);
    The for statement should be along the lines of for(;;final==0)

    Finally remove all the semicolons in your if statements, aka remove the ";"
    Code:
    if(myguess>compguess);

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    The for statement should be along the lines of for(;;final==0)
    No. The middle portion of the loop is where it checks to see if it continues looping. Where you moved it to would be the incrementation part, and as such, that statement would have no effect.
    Code:
    for( initialize; test; increment )

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That is what I thought originally but when I tried to compile it I had an epic fail. Perhaps the compiler I am using is dropping this down to a while statement?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What epic fail might that be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Laser, you are on, great. I tried the program initially as qzuah stated with the for (;final==0 and the program just ended. I then tried it with for(;;final==0) and the program runs through. I am not sure what is going on here.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, now try removing the final == 0 entirely
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    ok, I give. Why did that work Laser?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    With an empty condition, you get an infinite loop. It looks like mentat's mistake with the condition is not that the condition is in the wrong place, but that it tests the opposite of what was intended, i.e., the loop is supposed to loop while final != 0, not while final == 0. Of course, other problems also should be fixed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Alright, so it was compiler specific then? When I ran the code I posted it appeared to work just like when I did it with the empty condition. Also I am a little bothered by the OP "for function" statement. My understanding is that is higher level languages things like for are more like logical loops than functions.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    Alright, so it was compiler specific then? When I ran the code I posted it appeared to work just like when I did it with the empty condition.
    No, it is not compiler specific. The code that you posted has an empty condition.

    Quote Originally Posted by AndrewHunter
    Also I am a little bothered by the OP "for function" statement. My understanding is that is higher level languages things like for are more like logical loops than functions.
    Yes, but I wouldn't generalise to "higher level languages" unnecessarily.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, I see. Thank you laser. I bow to your C omnipotence.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Location
    Vancouver, British Columbia, Canada
    Posts
    4
    Thank you for all the input guys! It was noobism of the highest degree which impeded this program from working, not a lack of understanding of 'for', as I thought. After I deleted the semi-colons and added the ampersands infront of the variable declarations in the scanf functions the code worked perfectly.

    Thanks again,

  14. #14
    Registered User
    Join Date
    Jun 2011
    Location
    Vancouver, British Columbia, Canada
    Posts
    4
    Just in case anyone feels like throwing a tidbit of information my way while I scrounge the interweb: I'm trying to find a function that will generate a random number between two int variables.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always try the FAQ: Cprogramming.com FAQ > Generate random numbers?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a beginner program using argc and argv
    By Hybodus in forum C Programming
    Replies: 1
    Last Post: 12-19-2010, 04:56 PM
  2. Noobie C question, quick answer
    By evandegr in forum C Programming
    Replies: 12
    Last Post: 11-06-2010, 01:45 PM
  3. stupid question need quick answer
    By mbsupermario in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 06:17 AM
  4. Quick Answer...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2002, 11:22 AM
  5. Need Answer Quick
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2002, 12:55 PM