Thread: simple question- not ruinning as expected

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    22

    simple question- not ruinning as expected

    hi, im just trying to make a simple program, just to see wat i can do.

    i want to no y this code doesnt run as expected, after the last line the program quits, without leaving time to view wat is supposed to be on the screen

    help please

    Code:
     #include <iostream>
    
    using namespace std; // So we can see cout and endl
    
    int main()
    { 
      int x = 0;  //  declare variables
      int answer;
      int y = 0;
      
    
    
      
      while ( x < 100000 ) { // While x is less than 100000
        cout<< x <<endl;
        x++; 
        x++;            // Update x so the condition can be met eventually
      }
    cout<< "Total files placed in your computer = " ;
    cout << "99899 \n";
    cout << "Happy holidays!.\n";
    
    cout<< "Do you think im telling the truth?\n";
    cout<< "1 = yes\n2 = no\n" ;
    cin>> answer; //stores variable in answer
      if (answer = 0) {
      cout<< "Well if your not going to believe me......\n maybe i should add some more... would you like that? \n";
      
    }
    
      //program should wait here, waiting for an input or enter stroke to quit
       
    
      cin.get();}

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Common problem. The code "cin >> answer" reads an integer from the input stream into the variable answer. The newline from when you hit Enter is still in the input stream. The cin.get() call reads in that newline instead of waiting for you to hit Enter again. Use two cin.get() calls, or add a cin.ignore() call before the cin.get() to ignore the newline that is still in the stream.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    while ( x < 100000 ) { // While x is less than 100000
    Do you really think that comment is necessary? Comments like that are as bad as no comments for difficult code.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    Think i must be doing something still wrong.
    or missing out something uve said.
    Code:
    #include <iostream>
    
    using namespace std; // So we can see cout and endl
    
    int main()
    { 
      int x = 0;  //  declare variables
      int answer;
      int y = 0;
      
    
    
      
      while ( x < 100000 ) { 
        cout<< x <<endl;
        x++; 
        x++;            // Update x so the condition can be met eventually
      }
    cout<< "Total files placed in your computer = " ;
    cout << "99899 \n";
    cout << "Happy holidays!.\n";
    
    cout<< "Do you think im telling the truth?\n";
    cout<< "1 = yes\n2 = no\n" ;
    cin.get(); //tried it here after the new line
    cin>> answer; //stores variable in answer
    
      if (answer = 0) {
      cout<< "Well if your not going to believe me......\n maybe i should add some more... would you like that? \n";
      
    }
    
      //program should wait here, waiting for an input or enter stroke to quit
       
    
    cin.get(); tried it here ( only 1 at a time)
      cin.get();}
    what am i missing out / doing wrong

    all help appreciated

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Here's some pretty heavily commented code for you. I cleaned up your text formatting, and tried to illustrate some concepts you should know.

    Code:
    #include <iostream>
    
    int main()
    {
    	// Let's replace your while() loop with a for() loop so it's more intuitive to read.
    	
    	// In a for() loop, you can declare a variable that will only be in that loop's scope.
    	// This is a GoodThing(tm). It helps your program not use as much memory.
    	
    	for (int x = 0; x < 10000; x++)
    	{ 
    		std::cout << x << std::endl;
    	}
    	
    	// std::cout is overloaded. You can string together multiple <<'s, and it helps readability most of the time.
    	
    	std::cout << "\nTotal files placed in your computer = " << "9999\n";
    	std::cout << "Happy holidays!\n\n";
    	
    	std::cout << "Do you think I'm telling the truth?\n\n";
    	
    	std::cout << "1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; // It's usually a good idea to let the user know you expect input.
    	
    	// Don't have an empty cin.get() here, you'll swallow your number with no effect.
    	
    	int answer; // It's usually a good idea to declare variables near the point of first use.
    	
    	std::cin >> answer;
    	
    	// Since we're just going to keep this simple, we'll assume the user plays nice and doesn't put
    	// anything but a number in from standard input.
    	
    	// Since the only possible answer that wouldn't display the message in the if{} block is 1,
    	// then we can just check for a non-1 answer.
    	
    	if (answer != 1)
    	{
    		std::cout << "\nWell if you're not going to believe me. . .\n";
    		std::cout << ". . .maybe I should add some more. Would you like that?\n";
    	}
    	
    	std::cin.ignore(80, '\n'); // This will flush the newline from stdin (the input buffer).
    	std::cin.get();
    
    	return(0); // ALWAYS RETURN A VALUE FROM MAIN(). It doesn't matter if main() implicitly returns or not.
    }

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    Thanks for that.
    Yeah i can understand most of that now and its proved helpfull
    the only thing is i tried adding an 'else if (answer = 1)' line, but compiler reckons there is something wrong

    all help has and will be apreciated





    Code:
    #include <iostream>
    
    int main()
    {
    	// Let's replace your while() loop with a for() loop so it's more intuitive to read.
    	
    	// In a for() loop, you can declare a variable that will only be in that loop's scope.
    	// This is a GoodThing(tm). It helps your program not use as much memory.
    	
    	for (int x = 0; x < 10000; x++)
    	{ 
    		std::cout << x << std::endl;
    	}
    	
    	// std::cout is overloaded. You can string together multiple <<'s, and it helps readability most of the time.
    	
    	std::cout << "\nTotal files placed in your computer = " << "9999\n";
    	std::cout << "Happy holidays!\n\n";
    	
    	std::cout << "Do you think I'm telling the truth?\n\n";
    	
    	std::cout << "1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; // good idea to let the user know you expect input.
    	
    	// Don't have an empty cin.get() here, you'll swallow your number with no effect.
    	
    	int answer; // good idea to declare variables near the point of first use.
    	
    	std::cin >> answer;
    	
    	// Since we're just going to keep this simple, we'll assume the user plays nice and doesn't put
    	// anything but a number in from standard input.
    	
    	// Since the only possible answer that wouldn't display the message in the if{} block is 1,
    	// then we can just check for a non-1 answer.
    	
    	if (answer != 1)
    	{
    		std::cout << "\nWell if you're not going to believe me. . .\n";
    		std::cout << ". . .maybe I should add some more. Would you like that?\n";
    	}
    	
    	std::cin.ignore(80, '\n'); // This will flush the newline from stdin (the input buffer).
    	
    	std::cout << "\n\n1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; // good idea to let the user know you expect input.
        
        else if (answer = 1)
    	{
             for (int y = 0; y < 100000; y++)
             {
                 std::cout << y << std::endl;
                 }
             std::cout << "\nGood....Thats very good. For i have now removed them all!\n";
             std::cout << "Press enter to quit\n";
             std::cin.get();
                 }
        std::cout << "\n\nAnd again please....\n\n"
        std::cin.get();
    
    	return(0); // ALWAYS RETURN A VALUE FROM MAIN(). It doesn't matter if main() implicitly returns or not.
    }

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    else if (answer = 1)
    Wrong. = is the assignment operator, == is the comparison operator.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    o lol, i thought when i saw that , that it was a silly little mistake, however,
    still recons theres something roung. even when i add the extra =



    Code:
    #include <iostream>
    
    int main()
    {
    	// Let's replace your while() loop with a for() loop so it's more intuitive to read.
    	
    	// In a for() loop, you can declare a variable that will only be in that loop's scope.
    	// This is a GoodThing(tm). It helps your program not use as much memory.
    	
    	for (int x = 0; x < 10000; x++)
    	{ 
    		std::cout << x << std::endl;
    	}
    	
    	// std::cout is overloaded. You can string together multiple <<'s, and it helps readability most of the time.
    	
    	std::cout << "\nTotal files placed in your computer = " << "9999\n";
    	std::cout << "Happy holidays!\n\n";
    	
    	std::cout << "Do you think I'm telling the truth?\n\n";
    	
    	std::cout << "1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; // good idea to let the user know you expect input.
    	
    	// Don't have an empty cin.get() here, you'll swallow your number with no effect.
    	
    	int answer; // good idea to declare variables near the point of first use.
    	
    	std::cin >> answer;
    	
    	// Since we're just going to keep this simple, we'll assume the user plays nice and doesn't put
    	// anything but a number in from standard input.
    	
    	// Since the only possible answer that wouldn't display the message in the if{} block is 1,
    	// then we can just check for a non-1 answer.
    	
    	if (answer != 1)
    	{
    		std::cout << "\nWell if you're not going to believe me. . .\n";
    		std::cout << ". . .maybe I should add some more. Would you like that?\n";
    	}
    	
    	std::cin.ignore(80, '\n'); // This will flush the newline from stdin (the input buffer).
    	
    	std::cout << "\n\n1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; // good idea to let the user know you expect input.
        
        else if (answer == 1)
    	{
             for (int y = 0; y < 100000; y++)
             {
                 std::cout << y << std::endl;
                 }
             std::cout << "\nGood....Thats very good. For i have now removed them all!\n";
             std::cout << "Press enter to quit\n";
             std::cin.get();
                 }
        std::cout << "\n\nAnd again please....\n\n"
        std::cin.get();
    
    	return(0); // ALWAYS RETURN A VALUE FROM MAIN(). It doesn't matter if main() implicitly returns or not.
    }

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Here is your fixed program, I'm pretty confident that this is how you want it to run.

    Code:
    #include <iostream>
    
    int main()
    {
    	for (int x = 0; x < 10000; x++)
    		std::cout << x << std::endl;
    	
    	std::cout << "\nTotal files placed in your computer = " << "9999\n";
    	std::cout << "Happy holidays!\n\n";
    	
    	std::cout << "Do you think I'm telling the truth?\n\n";
    	
    	std::cout << "1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; 
    	
    	int answer;
    	
    	std::cin >> answer;
    	std::cin.get();	//catch enter input
    	
    	if (answer != 1)
    	{
    		std::cout << "\nWell if you're not going to believe me. . .\n";
    		std::cout << ". . .maybe I should add some more. Would you like that?\n";	
            
        std::cout << "\n\n1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> ";
    	std::cin >> answer;   //you forgot to get the answer for the 2nd question
    	std::cin.get();            //catch the enter input
        }
    
        if (answer == 1)
       {
             for (int y = 0; y < 100000; y++)
             {
                 std::cout << y << std::endl;
             }
             std::cout << "\nGood....Thats very good. For i have now removed them all!\n";
             std::cout << "Press enter to quit\n";
             std::cin.get();
        }
        std::cout << "\n\nAnd again please....\n\n";
        std::cin.get();
    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    nope sorry lol.
    this is my fault- by half completing a prgram.
    the
    Code:
    else if (answer == 1)
    line was supposed to do wateva i put in- if the original answer was 1. i left the
    Code:
    if
    only half completed, because i knew this worked and would add more later.

    if this makes no sense, tell me and i will annotate code

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    then there is no use for the else part, because it isnt paired up with an if statement. plus dont you want the input for the second question?

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    Instead of ignore, you can use cin.get() to catch the return key after each input.

  13. #13
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Lemme add a few touch-ups for you sir.
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    { 
      int x=0,y=0,answer=0; //  declare variables
      
      while ( x < 100000 ) { // While x is less than 100000
        cout<< x <<endl;
        x+=2;  // Update x so the condition can be met eventually
      }
          cout<< "Total files placed in your computer = 99899 \n" ;
          cout << "Happy holidays!.\n";
    
          cout<< "Do you think im telling the truth?\n";
          cout<< "1 = yes\n2 = no"<<endl; //Flushing ze buffer is good, ya?
          cin>> answer; //stores variable in answer
               if (answer == 0) {
                     cout<< "Well if you're not going to believe me......\n
                            maybe i should add some more... would you like that? \n";
      
                  }
    
      //program should wait here, waiting for an input or enter stroke to quit
       
      cin.ignore();
      cin.get();
      return 0;
    }
    Last edited by Krak; 05-17-2005 at 09:51 PM.

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    Im not making my self clear enough, apoligies.

    I hope this code will help explain myself.

    Code:
    #include <iostream>
    
    int main()
    {
    	// Let's replace your while() loop with a for() loop so it's more intuitive to read.
    	
    	// In a for() loop, you can declare a variable that will only be in that loop's scope.
    	// This is a GoodThing(tm). It helps your program not use as much memory.
    	
    	for (int x = 0; x < 10000; x++)
    	{ 
    		std::cout << x << std::endl;
    	}
    	
    	// std::cout is overloaded. You can string together multiple <<'s, and it helps readability most of the time.
    	
    	std::cout << "\nTotal files placed in your computer = " << "9999\n";
    	std::cout << "Happy holidays!\n\n";
    	
    	std::cout << "Do you think I'm telling the truth?\n\n";
    	
    	std::cout << "1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; // good idea to let the user know you expect input.
    	
    	
    	int answer; // good idea to declare variables near the point of first use.
    	
    	std::cin >> answer;
    	
    	
    	
    	if (answer != 1)
    	{
    		std::cout << "\nWell if you're not going to believe me. . .\n";
    		std::cout << ". . .maybe I should add some more. Would you like that?\n";
    	}
    	
    	std::cin.ignore(80, '\n'); // This will flush the newline from stdin (the input buffer).
    	
    	std::cout << "\n\n1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> "; 
    	//this is half completed, i was going to cacth variable, and act upon it later.
        
        else if (answer == 1)
        // this else if, is still acting upon the first variable , x,.
        //the first stated code to be completed if ,x, was not 1.
        // now i am stating code which should be completed, if ,x, does = 1
    	{
             for (int y = 0; y < 100000; y++)
             {
                 std::cout << y << std::endl;
                 }
             std::cout << "\nGood....Thats very good. For i have now removed them all!\n";
             std::cout << "Press enter to quit\n";
             std::cin.get();
                 }
        std::cout << "\n\nAnd again please....\n\n"
        std::cin.get();
    
    	return(0); // ALWAYS RETURN A VALUE FROM MAIN().

  15. #15
    *this
    Join Date
    Mar 2005
    Posts
    498
    does it work the way you want it to?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM