What is wrong with my code?

This is a discussion on What is wrong with my code? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get it to work like if I earned 5 apples but I only earned 2 - I ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    What is wrong with my code?

    I'm trying to get it to work like if I earned 5 apples but I only earned 2 - I want it to be able to detect no and yes and let me either input the right amount or go to the next question.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int thisisanumber;
        
        cout<<"How many apples have you earned?";
        cin>> thisisanumber;
        cin.ignore() ;
        cout<<"You entered: "<< thisisanumber <<"\n";
        
        int thisisno;
        int thisisyes;
        
        cout<<"Is this amount correct?"<< thisisno <<"\n";
        cin>> thisisno;
        cin>> thisisyes;
        cin.ignore() ;
        cout<<"Please enter the correct amount:"<< thisisno <<"\n";
        cout<<"How many grapes have you earned?"<< thisisyes <<"\n";
        cin.get();
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    A few things wrong with your code here.
    1) You're using thisisno to print the value to the screen, yet you never gave it a value! (called initialization)
    2) How are you suppose to detect yes or no when you never did any comparisons? ie. if statements, strcmp, strncmp, etc.
    3) Why use two variables for yes and no? if you enter a 1 for "is this correct" then it is believed to be correct, and 0 would be incorrect. Don't need to check two things for one question here.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int thisisanumber;
        
        cout<<"How many apples have you earned?";
        cin>> thisisanumber;
        cin.ignore() ;
        cout<<"You entered: "<< thisisanumber <<"\n";
        
        int thisisno;
        int thisisyes;
        
        cout<<"Is this amount correct?"<< thisisno <<"\n";
        cin>> thisisno;
        cin>> thisisyes;
        cin.ignore() ; // need a comparison somewhere around here
        cout<<"Please enter the correct amount:"<< thisisno <<"\n";
        cout<<"How many grapes have you earned?"<< thisisyes <<"\n";
        cin.get();
    }

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    2
    Thanks, I was trying to mess around with some code from a tutorial and I got way ahead of myself!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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