Program doesn't work as it should.

This is a discussion on Program doesn't work as it should. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cctype> using namespace std; int main() { cout << "Enter whole numbers that you want me ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    66

    Program doesn't work as it should.

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
        cout << "Enter whole numbers that you want me to total up:"
             << endl;
        
        int value = 0;                                        //value to be added
        int total = 0;                                        //total of all values
        char yesno = 0;                                       //check if user wants to continue
        
        do
        {
            cout << "Enter a whole number: ";                  //let user enter number       
            cin >> value;
            
            total += value;                                        //add value to total
            
            cout << "Do you want to add some more numbers? (y/n) ";        //check if user wants to add more numbers
            cin >> yesno;        
            
            while (tolower(yesno) != ('y'||'n'))                         //tell user that they have entered wrong value
            {
                    cout << "You have entered an invalid answer. Please try again (y/n): ";
                    cin >> yesno;               
            }            
            
        }while (tolower(yesno) == 'y');                          //loop stops if user doesn't enter 'y'
            
        cout << endl                                            //tell user the total
             << "The total is "
             << total
             << "."
             << endl
             << endl;
        
        system("PAUSE");
        return 0;
    }
    The part in red seems to be the problem. The loop goes on and on even if i enter 'y' or 'n' . What's wrong?

    P.S one more questions: wat does header file <cstdlib> and <cstdio> do? The first book I read 'bout C++ seems to 'enforce' the two header files in all the example programs. But in the book <<Ivor Horton's Beginning C++ The complete language>> by the Wrox company , they only put <iostream> for the basic programs. I'm on to the chapter 'bout loops, but headers file they have used so far are: <iostream> <limits> <cctype> <iomanip> . Can someone explain?
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I believe it's your While Statement.
    Instead of:
    Code:
    while (tolower(yesno) != ('y'||'n'))
    Try:
    Code:
    while ((tolower(yesno) != 'y') || (tolower(yesno) != 'n'))
    Not gauranteed, but I'm pretty sure you have to split them up like that.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by Epo
    I believe it's your While Statement.
    Instead of:
    Code:
    while (tolower(yesno) != ('y'||'n'))
    Try:
    Code:
    while ((tolower(yesno) != 'y') || (tolower(yesno) != 'n'))
    Not gauranteed, but I'm pretty sure you have to split them up like that.
    Tried it. Didn't make a difference.
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Have you tried stepping through to see the contents of 'yesno'?

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    thought of that....but too dumb to know how to work the debugger ><
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    while ((tolower(yesno) != 'y') && (tolower(yesno) != 'n'))
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Ah yes. I feel like a retarded child in grade 1. I'm gonna go back to my corner.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by Epo
    Ah yes. I feel like a retarded child in grade 1. I'm gonna go back to my corner.
    sigh....same here

    btw, can someone answer my PS question?

    one more questions: wat does header file <cstdlib> and <cstdio> do? The first book I read 'bout C++ seems to 'enforce' the two header files in all the example programs. But in the book <<Ivor Horton's Beginning C++ The complete language>> by the Wrox company , they only put <iostream> for the basic programs. I'm on to the chapter 'bout loops, but headers file they have used so far are: <iostream> <limits> <cctype> <iomanip> . Can someone explain?
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Any C++ reference will tell you what the various library headers do. <iostream> is used for C++ I/O. If your previous examples used <cstdio> then they probably use C I/O (printf, scanf etc) rather than cout and cin. <cstdio> is the C++ library version of C's <stdio.h> header.

    Are you sure your C++ books don't have a section on headers somewhere? Might be worth looking in the index.

  10. #10
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    It does, except that I dun understand it...
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 11:37 AM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 10:05 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 12:48 PM

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