CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: quick question

  1. #1
    Join Date
    Mar 2010
    Posts
    46

    quick question

    I am writing a program that uses if statements. The program will prompt the user to enter 2 numbers 0-100. If Num1 is > Num2 it needs to output "Num1 is greater than Num2. Num1 is the greater variable." If Num1 < Num2 "Num2 is greater than Num1. Num2 is the greater variable." my question is this

    how to i get the program to print the numbers the user inputs and not Num1 and Num2. here is the source any feedback is greatly appreaciated. It always seems to be the simple stuff that confuses me.

    #include <iostream>

    using namespace std;

    int main()

    {
    int Num1;
    int Num2;

    cout << "Please enter a number 0-100 ";
    cin >> Num1;
    cout << endl;
    cout << "Please enter a number between 0-100 ";
    cin >> Num2;
    cout << endl;
    if (Num1 > Num2)
    cout << "Num1 is greater than Num2. Your first variable was greater" << endl;
    else (Num1 < Num2);
    cout << "Num2 is greater that Num1. Your second variable is greater" << endl;
    return 0;

    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: quick question

    Your else statement should not have a condition after it, nor should it have a semicolon.

    What happens if the two numbers are equal?

    To your specific question, to output a variable called Num1, just do
    Code:
    cout << Num1;
    You can chain this together with other outputs (such as literal strings in "") using additional insertion operators (<<).

  3. #3
    Join Date
    Mar 2010
    Posts
    46

    Re: quick question

    Lindley,

    Should I be using to if statments then?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: quick question

    Well, you might want to throw in an else if for the equal case.

  5. #5
    Join Date
    Mar 2010
    Posts
    46

    Re: quick question

    Lindley Thanks and I will add that but I am having a problem. I got the program to run but here is the quarrel. I used 75 and 5 as my control numbers. If I enter 5 and 75 the program works correctly. but if i enter 75 and 5 it says

    75 is greater than 5 ...... which is good
    it also says
    75 is les than 5......... which is not good

    here is the source I wrote

    #include <iostream>

    using namespace std;

    int main()

    {
    int Num1;
    int Num2;

    cout << "Please enter a number 0-100 "; //prompts user to enter a the first number

    cin >> Num1;

    cout << endl;

    cout << "Please enter a number between 0-100 "; // prompts user to enter second number

    cin >> Num2;

    cout << endl;

    if (Num1 > Num2) // If statment processes which number is greater.

    cout << Num1 << " is greater than " << Num2 << " Your first variable was greater " << endl; //prints to screen

    if (Num1 < Num2); // If statment processes which number is greater

    cout << Num1 << " is less than " << Num2 << " Your second variable is greater " << endl; // prints to screen

    return 0;

    }

  6. #6
    Join Date
    Jun 2005
    Posts
    315

    Re: quick question

    Code:
    if (Num1 < Num2); // If statment processes which number is greater
    Try getting rid of the semi colon in this line.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: quick question

    Quote Originally Posted by AKGROWN View Post
    Lindley Thanks and I will add that but I am having a problem. I got the program to run but here is the quarrel. I used 75 and 5 as my control numbers. If I enter 5 and 75 the program works correctly. but if i enter 75 and 5 it says
    Please use code tags when posting code.
    Code:
    if (Num1 < Num2); // If statment processes which number is greater
    What is that semicolon doing at the end of the line? That is the problem.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Mar 2010
    Posts
    46

    Re: quick question

    yeah i figured it out. when i was first built the solution it said missig ; before so i assumed thats where it was supposed to go. I just finished the assignment and was able to figure the last part out all by myself which is a freaking act of god. here is the final program in its entirety.

    //This program will prompt the use to enter 2 numbers.
    //it will then process the information and print the
    //larger number to the screen. The program will prompt
    //the user for a number 1-12, process the information
    // and print the corrosponding month to the screen.


    #include <iostream>

    using namespace std;

    int main()

    {
    int Num1;
    int Num2;
    int Num;

    cout << "Please enter a number 0-100 "; //prompts user to enter a the first number

    cin >> Num1;

    cout << endl;

    cout << "Please enter a number between 0-100 "; // prompts user to enter second number

    cin >> Num2;

    cout << endl;

    if (Num1 > Num2) // If statment processes which number is greater.

    cout << Num1 << " is greater than " << Num2 << " Your first variable was greater " << endl; //prints to screen

    if (Num1 < Num2) // If statment processes which number is greater

    cout << Num1 << " is less than " << Num2 << " Your second variable is greater " << endl; // prints to screen

    else if (Num1 == Num2) // Processes whether the entered values are equal

    cout << Num1 << " is equal to " << Num2 << " The values entered are equal " << endl;

    cout << "Enter a number from 1-12: "; //prompts user to enter a value from 1-12

    cin >> Num; // users value

    cout << "The number you entered is " << Num << endl; // prints the user entered value to the screen

    switch (Num) // switch statment processes which value to to match to corrosponding entered value
    {
    case 1:
    cout << "January" << endl;
    break ;
    case 2:
    cout << "Februrary" << endl;
    break;
    case 3:
    cout << "March" << endl;
    break;
    case 4:
    cout << "April" << endl;
    break;
    case 5:
    cout << "May" << endl;
    break;
    case 6:
    cout << "June" << endl;
    break;
    case 7:
    cout << "July" << endl;
    break ;
    case 8:
    cout << "August" << endl;
    break ;
    case 9:
    cout << "September" << endl;
    break ;
    case 10:
    cout << "October" << endl;
    break ;
    case 11:
    cout << "November" << endl;
    break ;
    case 12:
    cout << "December" << endl;
    break;
    default:
    cout << "That is not a valid number" << endl; //error message to tell user he/she has entered an invalid value
    }

    return 0; //terminates program

    }

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: quick question

    A good start. If you haven't been shown them already, soon you'll learn how to use arrays to let you avoid such annoyingly verbose switch statements.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: quick question

    Quote Originally Posted by AKGROWN View Post
    yeah i figured it out. when i was first built the solution it said missig ; before so i assumed thats where it was supposed to go. I just finished the assignment and was able to figure the last part out all by myself which is a freaking act of god. here is the final program in its entirety.
    How hard is it to type

    [code] [/code]

    around your code? If you're going to post code, at least post it so that it is readable.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured