Thread: Help

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    Help

    Can someone that is good with programming C++ (entry level programming)
    Help me with this problem: (also see attachment)

    Write a program that reads two integers num1 and num2 from the user, then displays the total of the all
    of the numbers between num1 and num2 (inclusive). Your program should handle the following:
    1. Make sure the numbers entered are between 0 and 100.
    2. Make sure the second number is greater than or equal to the first number entered.

    Note that the first number the user enters is the low end of the range for the second number. So, as an
    additional example:
    Please enter an integer between 0 and 100: 25
    Please enter an integer between 25 and 100: 75
    The total of the integers between 25 and 75 is 2550
    Note that you have to CHECK THAT THE NUMBERS ARE IN RANGE BEFORE TRYING TO DO THE LOOP!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Post the code you have started with.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    I don't have access to private message. can you message me?

  4. #4
    i've lost my mind
    Join Date
    Jun 2008
    Posts
    26
    Use the forum.

  5. #5
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    you should say to your professor at school that using system pause is a bad habit

    if he has any objections direct him to this site:

    IP Banned - GIDNetwork

  6. #6
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    all I don't understand what operator should I use between num1 and num2

    I did your homework in less than 30 min ---->>> BUT I'M NOT GIVING YOU JUST LIKE THAT

    learn C++

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Ok, I am completely stuck, as I don't really fully understand integers.

    so far I have:
    -----------------------------------------------------------

    #include <iostream>

    using namespace std;

    int main()
    {
    int num1;
    int num2;

    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;

    cout << "Please enter an integer between 50 and 100: ";
    cin >> num2;

    integer counter = num1, total = 0;

    while (counter <= num2)

    total = total + counter;
    counter++;

    cout << "The total of the intergers between " << num1 << " and " << num2 << " is " << total << end;


    ----------------
    I am hoping I am on the right path, but I have to incorporate the "IF's" in there as well if the numbers are not in range and should say " Please read and follow the directions!"

    So I am thinking somewhere in there, I have to add:

    IF num1 < 1 or > 100 then cout << "Please read and follow the directions!"

    and

    If num2 <50 or >100 then cout << "Please read and follow the directions!"



    I am stuck and running out of time.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    first: you don't have braces around the body of your while loop.

    second: why are you looping a subtraction operation?

  9. #9
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    Code:
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
    
    cout << "Please enter an integer between 50 and 100: ";
    what about if I press 30 in the first num1???

    than it wouldn't be 50 right

    you must correct there that it will show you the variable you have pressed

    try to repair that and show your code again

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you don't understand integers, you're probably in the wrong class.

    Now, you're on the right track with your if statements, but I would suggest you follow your own advice and do what your cout statements say, because:
    Quote Originally Posted by your pseudocode
    If num2 <50 or >100 then cout << "Please read and follow the directions!"
    is not the same thing as
    Quote Originally Posted by the assignment
    Make sure the second number is greater than or equal to the first number entered.
    Emphasis mine.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Its not a subtraction problem. In my first post, see the attachment of the assignment description.

    As far as the brackets, I realize I am missing those, I am looking for help with the total picture, and getting it right.

  12. #12
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you should use a for loop.

    Code:
    int total;
    for(int counter=num1;counter<num2;counter++)
    {
        total+=counter;
    }
    put your if statements between this and the input prompts. you should be familiar with inequality operations; they equate to either true or false conditions. the logic of using an if statement is very simple:

    Code:
    if(condition)
    {
       statements
    }

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    You guys are great. thank you. Ok, so this is what I have:
    -------------------------------------------
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    int num1;
    int num2;
    
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
    
    cout << "Please enter an integer between 50 and 100: ";
    cin >> num2;
    
    int total;
    for(int counter=num1;counter<num2;counter++)
    {
        total+=counter;
    }
    {
    if num1 < 1 or > 100 cout << "Please read and follow the directions!"  
    }
    {
    If num2 <50 or >100 cout << "Please read and follow the directions!"
    }
    
    cout << "The total of the intergers between " << num1 << " and " << num2 << " is " << total << end;
    -------------------------------------------


    Am I correct? My assignment is attached to my first post here. I just want to make sure I have everything perfect to all the instructions.

    thank!
    Last edited by Titanguy; 10-14-2009 at 10:01 AM.

  14. #14
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    no, your syntax is not correct.

    as a matter of principle, your if statements should proceed your loop also. why compute an invalid number?

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    You lost me... I am brand new to this. please explain

Popular pages Recent additions subscribe to a feed