Thread: basic code help

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    basic code help

    Hey, I am new to programming and I was trying to make a basic add program. It is working fine until it asks you if you want to do it again and no matter if I push y or n it does nothing. What am I missing?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult (int a, int b);
    
    int main()       //Trying to make a basic add program. After you add will ask if you want 
    {                //to do another problem but isn't. to try and simplize the program erased 
        int a;       //code to make program repeat when you do again and simply put it works 
        int b;       //until i can find out why if statement is not working. 
        char  y, n;
        
        cout<<"Please input the two numbers to be added: ";  
        cin>> a >> b;
        cin.ignore();
        cout<<"  The sum of the two numbers is: "<< mult (a, b) <<"\n";
        cin.get();
        cout<<"Would you like to add again? (y/n): ";
        cin>> y >> n;
        cin.ignore();
        if ( y == y )  {
        cout<<"It works\n";
        }
    
        
    }
    int mult (int a, int b)
        {
             return a + b;
        }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    int a = 0, b = 0;
    char again = 'y'; // Well the user is only going to be making one choice
                      // so it needs just one variable
    while(again != 'n') {    // make sure you compare it to a character
       cout<< "Please enter two digits to add: ";
       cin>> a >>b;
       cout<< "The sum is " <<add(a, b) <<endl;
       cout<< "Got another problem? ";
       cin>> again;
    }
    Something like that.
    Last edited by whiteflags; 05-20-2006 at 01:51 AM.

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    when you ask the user if they want to repeat, you only need to input into one char and check to see if that's 'y'. so get rid of the n variable, change cin >> y >> n; to cin >> y; and then your if statement, put single quotes around the second y so that it compares the contents of the y variable to the character y. if (y == 'y'). otherwise it checks to see if y is equal to itself, which will always be true.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> if ( y == y )

    should be

    if ( y == 'y' )

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    citizen's code it a perfect candidate for a do-while loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Basic Code Help
    By Slinger137 in forum Windows Programming
    Replies: 9
    Last Post: 01-04-2009, 10:26 AM
  2. Basic Code
    By MadCow257 in forum Game Programming
    Replies: 6
    Last Post: 03-01-2005, 05:28 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM