Program to determine sum

This is a discussion on Program to determine sum within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by jadedreality Okay I've changed it around a little, it works but not like it's supposed to...when I ...

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by jadedreality View Post
    Okay I've changed it around a little, it works but not like it's supposed to...when I press 1 for Yes it exits instead of letting me try again.


    Code:
    #include    <iostream>      
    #include    <ostream>       
    #include    <istream>       
    
    int main()
    {
    	char  y;
    
    
        int  a;      
        int  b; 
    	int  c;
    	
    
        std::cout
            << "This program will display the sum of three numbers." << std::endl;  
    
    	while (1 == Y);	{
    
        std::cout << "Enter an integer: ";
        std::cin >> a;
    
        std::cout << "Enter an integer: ";
        std::cin >> b;
    
    	std::cout << "Enter an integer: ";
        std::cin >> c;
    
        std::cout << "The sum is " << a + b + c << "." << std::endl;
    
    	std::cout << "Would you like to try again? 1 for Yes, 2 for No. " << std::endl;
    
    	}
    	
    
    return 0;
    	
    }
    When read 1 as a character it is represented by the character '1', which is distinct from the numeral 1. So you should check if '1' == Y.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> while (1 == Y);

    Ok, you're closer to the syntax, but still not quite right. There's something there that shouldn't be there.

    Also, as King Mir mentioned, if the variable Y is a char, then compare it against '1'. If the variable Y is an int, like how avgprogamerjoe used, then you can compare it against 1.

  3. #18
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Code:
    #include    <iostream>      
    //#include    <ostream>    Only Wikipedia will tell you to include these two      
    //#include    <istream>       
    
    int main()
    {
    	char  y = '1';   //initialize this variable, so that when you check it later, it has meaning.
        int  a, b,c;
    
        std::cout
            << "This program will display the sum of three numbers." << std::endl;  
    
    	while (y == '1') 	{   //the character constant '1', which is true the first time, since we set y to '1', remember? Otherwise this loop wouldn't run at all. 
    
        std::cout << "Enter an integer: ";
        std::cin >> a;
    
        std::cout << "Enter an integer: ";
        std::cin >> b;
    
    	std::cout << "Enter an integer: ";
        std::cin >> c;
    
        std::cout << "The sum is " << a + b + c << "." << std::endl;
    
    	std::cout << "Would you like to try again? 1 for Yes, 2 for No. " << std::endl;
            std::cin >> y;   //You have to take input, otherwise how do we know what the user wanted?
    
    	}
    	
    
    return 0;
    	
    }
    I suggest you follow some more tutorials. There are many on the internet, and the more you read the better of a grasp you'll have over what the code you're trying is actually doing.

    As for this program, I might make a few minor design changes.

    - Call y something like go_on, and have it take the character values 'y' or 'n'.
    - Use a do-while loop in the place of your while loop (look it up). It will improve readability and eliminate the mysterious initialization to 'y' or '1' at the beginning of your program.

    Happy programming.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maxium sum in a subvecter
    By sniperwire in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 06:33 PM
  2. Tic Tac Toe program
    By muzihc in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 08:08 PM
  3. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  4. Replies: 7
    Last Post: 05-26-2003, 05:44 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21