C++ Programming Question cin >>

Discussion in 'OT Technology' started by L Jay, Feb 14, 2009.

  1. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I'm creating a program for my programming class that uses user input to calculate and display investment statistics, I understand the concepts for the most part I'm just having trouble trying to figure out how once a user inputs a letter how to assign the value that is shown next to the letter to the variable

    Maybe the code will help bear in mind I'm only 10% done with it at the most.

    My fail values are suppose to be 5000 for the ammount and 5 years for the term if the user enters something outside the range. I'm testing their values with the bool.

    Once I figure out how to assign the values to the respective letters the rest of the coding should be pretty simple, I believe the professor is trying to get us to use if else if statements.

    I'm basically trying to see if I'm heading in the right direction, I don't have class till Wednesday and I would like to get at least 75% of the assignment done before class.

    Here is the code

    #include<iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    // Declare program variables for user input.
    char investamt = 5000.00;
    int years = 5.0;
    bool dataAreOK;

    cout << "Choose Investment Ammount" << endl
    << "-------------------------" << endl
    << "A ....... 5000" << endl
    << "B ....... 10000" << endl
    << "C ....... 15000" << endl
    << "D ....... 20000" << endl
    << "E ....... 25000" << endl
    << "Enter option A-E:" << endl;

    cin >> investamt;

    cout << "Enter Number of Years (5-30):";
    cin >> years;
    {
    // Test Data
    if (investamt <0 && years <0)
    dataAreOK = false;
    else
    dataAreOK = true;

    if(dataAreOK)


    return 0;
    }
     
  2. CodeX

    CodeX Guest

    don't use letters, use numbers, much easier to work with.

    do something like this:
    cout << "Choose Investment Ammount" << endl
    << "-------------------------" << endl
    << "1: ....... 5000" << endl
    << "2: ....... 10000" << endl
    << "3: ....... 15000" << endl
    << "4: ....... 20000" << endl
    << "5: ....... 25000" << endl
    << "Enter option A-E:" << endl;

    cin >> investment;

    switch(investment)
    {
    case 1: investment = 5000; break;
    case 2: investment = 10000; break;
    case 3: investment = 15000; break;
    case 4: investment = 20000; break;
    case 5: investment = 25000; break;
    }

    OR...

    Just ask them to input the actual number... Then check to make sure the number is in range before using it, if its not, loop back and ask them again.

    If you really want to use the capital letter to make the selection you have to compare the byte that was input into the investment variable with the ASCII character codes for those letter... or use a function in the standard library that does it for you, something like atoi() used like:
    if(investment == atoi("A")) whatever...;
    or even strcmp() like this:
    if(!strcmp(investment, "A")) whatever...;

    ALSO:

    char investment = 5000.00;

    makes zero sense. The char(acter) data type is an 8 bit integer value. Not only is the 5000.00 you are assigning to it overflowing the 8 bits but it is also a floating point number, integer data types do not represent fractional components.

    ADDITIONALLY:

    You do not need to preload the investment or year variables with any value because the first time you are referring to it is an assignment, and before that it doesn't matter what its value is.
     
    Last edited by a moderator: Feb 14, 2009
  3. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    Thanks though I have no choice, he basically gave me a sample ouput and I have to re-create it. :hs:


    Valid Input
    [FONT=&quot] Choose Investment Amount[/FONT] [FONT=&quot] ------------------------[/FONT]
    [FONT=&quot] A ....... 5000[/FONT]
    [FONT=&quot] B ....... 10000[/FONT]
    [FONT=&quot] C ....... 15000[/FONT]
    [FONT=&quot] D ....... 20000[/FONT]
    [FONT=&quot] E ....... 25000[/FONT]
    [FONT=&quot] ------------------------[/FONT]
    [FONT=&quot] Enter option A-E: D[/FONT]

    [FONT=&quot] Enter Number of Years (5-30): 20[/FONT]

    [FONT=&quot] Investment Summary[/FONT]
    [FONT=&quot] ------------------------------ [/FONT]
    [FONT=&quot] Number of Years: 20[/FONT]
    [FONT=&quot] Interest Rate: 4.50[/FONT]
    [FONT=&quot] Initial Investment: 20000.00[/FONT]
    [FONT=&quot] Interest Earned: 28234.28[/FONT]
    [FONT=&quot] Final Amount: 48234.28[/FONT]

    [FONT=&quot]It would take approximately 16 years[/FONT]
    [FONT=&quot]to double your initial investment.[/FONT]




    Invalid Input
    [FONT=&quot] Choose Investment Amount[/FONT]
    [FONT=&quot] ------------------------[/FONT]
    [FONT=&quot] A ....... 5000[/FONT]
    [FONT=&quot] B ....... 10000[/FONT]
    [FONT=&quot] C ....... 15000[/FONT]
    [FONT=&quot] D ....... 20000[/FONT]
    [FONT=&quot] E ....... 25000[/FONT]
    [FONT=&quot] ------------------------[/FONT]
    [FONT=&quot] Enter option A-E: f[/FONT]

    [FONT=&quot]Invalid menu choice.[/FONT]
    [FONT=&quot]Using 5000.00 for investment amount.[/FONT]

    [FONT=&quot]Enter Number of Years (5-30): 35[/FONT]

    [FONT=&quot]Years entered is not in range.[/FONT]
    [FONT=&quot]Using 5 years to invest.[/FONT]

    [FONT=&quot] Investment Summary[/FONT]
    [FONT=&quot] ------------------------------[/FONT]
    [FONT=&quot] Number of Years: 5[/FONT]
    [FONT=&quot] Interest Rate: 3.00[/FONT]
    [FONT=&quot] Initial Investment: 5000.00[/FONT]
    [FONT=&quot] Interest Earned: 796.37[/FONT]
    [FONT=&quot] Final Amount: 5796.37[/FONT]

    [FONT=&quot]It would take approximately 23 years[/FONT]
    [FONT=&quot]to double your initial investment.
    [/FONT]



    [FONT=&quot]Though I agree it would be a hell of alot easier doing it your way
    [/FONT]
     
  4. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    i don't see how using numbers is any easier than just doing the switch statement with a char instead.
     
  5. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    It's not the fact that its easier, I'm just following the guidlines for the assignment

    Obtain input from the user for the initial investment using a menu for choices of 5000, 10000, 15000, 20000, and 25000 dollars. Match the user’s response with menu choices to determine the corresponding value that will be used in the program. Assign a default value of 5000 dollars for input not matching one of the choices. Obtain input from the user for the number of years that the investment will accumulate interest. If that input is outside of the required range of 5 – 30 years, assign a default value of 5 years.
     
  6. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    i know, i was responding to codex's suggestion that the numbers would be easier than characters.
     
  7. CodeX

    CodeX Guest

    uhh, how about because he is asking the user for NUMBERS...

    the choices are 5000, 10000, 15000, 20000, 25000...
    why you would want to input those using A,B,C,D,E is beyond me, just ask them for the damn number. If you really are restricted to those 5 choices than simply state that in the question and check to make sure the number they entered was one of them. Why this roundabout way of picking a numbered choice when the choices are all numbers themselves is what I don't understand... It's not like you are choosing dialog options or anything, for example this would be understandable:

    "Please select from the following:
    1 - Drink a beer
    2 - Go to sleep
    3 - Play more fallout 3
    4 - Keep posting on OT

    See, the choices are text strings, so you represent them and make them easy to select with a ranked number representation, which makes sense. But, the input you want is a number already, so just ask them for the number...

    But I understand if that what the assignment asked, I just think it's a dumb way to do it.... Now, which choice do I make... :rofl:
     
  8. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    Do you know of a good if then else tutorial. The book I have is pretty shitty.

    I know I would set it up so that it reads
    if the investamt = 'A' the i would want it to assign the value to it

    investamt = 5000

    correct?
     
  9. CodeX

    CodeX Guest

    see, again here is your problem, you declared investamt as a char type, which is 8 bits. 8 bits has an unsigned range of 0 to 255... meaning if you try to put 5000 into it it will overflow, many times... so you can't just say:
    "if(investamt == 'A') investamt = 5000;

    Which is what you want to do, but after that assignment investamt is going to equal 136, which is the low 8 bits of the number 5000:

    5000 decimal = 1001110001000 binary, lowest 8 bits = 10001000 = 136 decimal.

    However, ignoring this problem and to demonstrate the logic of an if statement for you this is what you would do:

    if(investamt == 'A' || investamt == 'a') investamt = 5000;
    else if(investamt == 'B' || investamt == 'b') investamt = 10000;
    else if(investamt == 'C' || investamt == 'c') investamt = 15000;
    else if(investamt == 'D' || investamt == 'd') investamt = 20000;
    else if(investamt == 'E' || investamt == 'e') investamt = 25000;

    So, to get around this problem just declare investamt as a unsigned 16 bit, which I believe is unsigned int or unsigned short (depending on your compiler). This will give you a range of 0 - 65535, which is plenty for the values you are using in this example, and the 8bit ASCII value will still work fine with it, it will just ignore the upper 8 bits on assignment, I don't think explicit typecasting is required but that again may be compiler dependant.
     
  10. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Gather the user data into a char variable, then compare it to A, B, C, D, E, and set the value of investamt accordingly.
    Code:
    char userChoice ='';
    cin >> userChoice;
    switch(userChoice)
    {
    case A: investamt = 5000; break;
    .
    .
    .
    }
    
    Like that.
     
  11. CodeX

    CodeX Guest

    Code:
    char userChoice ='';
    cin >> userChoice;
    switch(userChoice)
    {
    case a:
    case A: investamt = 5000; break;
    case b:
    case B: investamt = 10000; break;
    .
    .
    .
    }
    
    fixed :mamoru:

    Those stupid users will kill you, you tell them to type a number and you look at the input log and see "a number"
     
  12. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I have no idea why I'm struggling with this so much.

    Here is my code


    #include<iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    // Declare program variables for user input.
    int investamt = 5000.00;
    int years = 5.0;
    bool dataAreOK;

    cout << "Choose Investment Ammount" << endl
    << "-------------------------" << endl
    << "A ....... 5000" << endl
    << "B ....... 10000" << endl
    << "C ....... 15000" << endl
    << "D ....... 20000" << endl
    << "E ....... 25000" << endl
    << "Enter option A-E:" << endl;

    cin >> investamt;


    if(investamt == 'A' || investamt == 'a') investamt = 5000;
    else if(investamt == 'B' || investamt == 'b') investamt = 10000;
    else if(investamt == 'C' || investamt == 'c') investamt = 15000;
    else if(investamt == 'D' || investamt == 'd') investamt = 20000;
    else if(investamt == 'E' || investamt == 'e') investamt = 25000;


    cout << "Enter Number of Years (5-30):" << endl;
    cin >> years;






    return 0;
    }

    For some reason everything works as it should, but it won't let me input my for my years variable.

    Also how would I test the input to see whether or not it is within the range of 5-30

    Here is a screen shot so you can see what I'm trying to explain, basically the program ends before I can input my number for years.

    Also, I'm using Visual Studio 2008 Professional as my compiler

    [​IMG]

    BTW I appreciate all the help guys
     
    Last edited: Feb 14, 2009
  13. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    cin is in a fail state since you entered a non-numerical character when it was expecting digits. You have to clear the fail state (cin.clear()) before cin accepts more input.

    The way around this is to have a seperate char variable to accept the user input for the investment amount choice, then set the investamt int accordingly.

    Give this a read for cin tips:
    http://www.augustcouncil.com/~tgibson/tutorial/iotips.html
     
  14. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    :h5:

    I just declared a second variable
    if(inputamt == 'A' || inputamt == 'a') investamt = 5000;

    Now how would I test to see if a value is within the given range?

    For example to see if my years where between 5 and 30?

    I'm assuming I would use a a boolean
     
  15. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    if (years > 5 && years < 30) cout << "my years are between 5 and 30";
     
  16. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    :wiggle:

    Thanks, now all I need to do is configure the formulas and output their results.
     
  17. CodeX

    CodeX Guest

    This is very nit-picky, but the above two lines are incorrect. They will likely compile just fine because you're compiler will just fix this for you, but you are assigning floating point numbers to integer variables. Data defined as an integer is not interpreted to have a fractional component.
     
  18. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    :uh:

    Your right, I actually fixed it because I was getting possible data loss warning.

    Thanks,
     
  19. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    Yet another problem,

    I set my if else statement to assign the interest rate to my variable based on the input, though no matter what the years is it always assigns the value 0.029999993

    What am I doing wrong?

    Here is the code, again :hs:

    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std;

    int main()
    {
    // Declare program variables for user input.
    int investamt = 5000;
    int years = 5;
    char inputamt;
    char inputyears;
    float interest;


    cout << "Choose Investment Ammount" << endl
    << "-------------------------" << endl
    << "A ....... 5000" << endl
    << "B ....... 10000" << endl
    << "C ....... 15000" << endl
    << "D ....... 20000" << endl
    << "E ....... 25000" << endl
    << "Enter option A-E:" << endl;


    cin >> inputamt;


    if(inputamt == 'A' || inputamt == 'a') investamt = 5000;

    else if(inputamt == 'B' || inputamt == 'b') investamt = 10000;

    else if(inputamt == 'C' || inputamt == 'c') investamt = 15000;

    else if(inputamt == 'D' || inputamt == 'd') investamt = 20000;

    else if(inputamt == 'E' || inputamt == 'e') investamt = 25000;

    if(investamt > 5000 || investamt> 25000) investamt =5000;

    cout << "Enter Number of Years (5-30): " << endl;

    cin >> inputyears;

    if (years < 5 && years > 30) years = 5;

    if(years >= 25) interest = 0.05;
    else if ( years >= 20 && years < 25) interest = 0.045;

    else if ( years >= 15 && years < 20 ) interest = 0.04;

    else if ( years >= 10 && years < 15) interest = 0.035;

    else if ( years < 10 && years >0) interest = 0.03;

    cout << " " << endl << setprecision (9) << interest << endl;


    return 0;
    }

    The cout << interest; is to display what was assigned to the interest variable.
     
  20. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Look again, more carefully, at what you're basing the decision on.
     
  21. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    :x: I got it, my variable should of been inputyears instead of years!

    Now it always outputs 0.050000007

    fuck

    Here is my logic statement

    if ( inputyears >= 25) interest = 0.05;
    else if ( inputyears <= 20 && inputyears >15) interest = 0.045;
    else if ( inputyears <= 15 && inputyears >10 ) interest = 0.04;
    else if ( inputyears <=10 && inputyears >15) interest = 0.035;
    else if ( inputyears < 10 && inputyears > 0) interest = 0.03;
     
    Last edited: Feb 15, 2009
  22. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    BTW here is what I'm basing my if statement off of

    25 years or more
    5%
    (0.05)
    20 years or more
    4.5%
    (0.045)
    15 years or more
    4%
    (0.04)
    10 years or more
    3.5%
    (0.035)
    less than 10 years
    3%
    (0.03)
     
  23. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    Code:
    if(inputamt == 'A' || inputamt == 'a') investamt = 5000;
        
    else if(inputamt == 'B' || inputamt == 'b') investamt = 10000;
    
    else if(inputamt == 'C' || inputamt == 'c') investamt = 15000;
    
    else if(inputamt == 'D' || inputamt == 'd') investamt = 20000;
    
    else if(inputamt == 'E' || inputamt == 'e') investamt = 25000;
    
    No final else? Arent you getting an error or warning about this?

    Code:
        
    if(investamt > 5000 || investamt> 25000) investamt =5000;
    
    This makes no sense,
    first test should probably be a < instead of >

    ...and even then why do this test at all?
    the variable is not storing user input so it wont ever contain an out of range value

    Code:
        
        if (years < 5 && years > 30) years = 5;
    
    this also makes no sense,
    basically says "if years is equal to two values at once"
    need to use 'or' ||


    Code:
    else if ( years < 10 && years >0)  interest = 0.03;     
    
    dont need to have the test for years > 0
    you already have a test earlier in your code getting rid of any values of years > 30 or < 5 and assigning the value of 5.
     
    Last edited: Feb 15, 2009
  24. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    What happens to values between 20 and 25?
    These values will never be caught

    and the third else if is also another one saying 'if inputyears is two values at once'...do this.
     
  25. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I was trying to right it so that it would assign the percentage to based on the range of years. I guess I failed. :hs:
     

Share This Page