Thread: help with a program for school

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    help with a program for school

    hey i have a quick question this program im doing for school wont work and me and my teacher cant figure out why perhaps someone could help me?

    // Memory card = 69.95
    // Software = 34.98

    #include <iostream.h>
    #include <string.h>
    int main()
    {
    float mem_card = (float) 0.00;
    float software = (float) 0.00;
    mem_card = 69.95;
    software = 34.98;
    float nw_pc = 675.00;
    float disk_drive = 198.50;
    float total;
    char item[30];
    int num_of;
    int times;
    /* int memory;
    int soft;
    int nw;
    int disk;*/

    /* cout << "How many Memory cards would you like to purchase? ";
    cin >> memory;
    cout << "How much software would you like to buy? ";
    cin >> soft;
    cout << "How many new pcs would you like to buy? ";
    cin >> nw;
    cout << "How many disk drives would you like to buy? ";
    cin >> disk; */
    cout << "How many items would you like to buy? ";
    cin >> times;
    for(int i = 1;i = times;i++)
    {
    cout << "Which items would you like to buy(Enter one item at a time then hit enter then type in how many you want to buy)? \n";
    cin.get(item,25);
    cin>>num_of;
    if(item == "memory card")
    total = total + (mem_card * num_of);
    else if(item == "software")
    total = total + (software * num_of);
    else if(item == "new pc")
    total = total + (nw_pc * num_of);
    else if(item == "disk drive")
    total = total + (disk_drive * num_of);
    else
    cout << "That is an invalid choice.\n";
    }
    cout << "Your total is " << total << endl;
    return 0;
    }
    Deleting intermediate files and output files for project 'projecthooch - Win32 Debug'.
    --------------------Configuration: projecthooch - Win32 Debug--------------------
    Compiling...
    projecthooch.cpp
    c:\projecthooch.cpp(10) : warning C4305: '=' : truncation from 'const double' to 'float'
    c:\projecthooch.cpp(11) : warning C4305: '=' : truncation from 'const double' to 'float'
    Linking...

    projecthooch.exe - 0 error(s), 2 warning(s)

    I have those warnings and what happens is when it gets to the second question it scrolls in an infinite loop somehow and i dont know how it does that? can anyone help me out? thanx
    hooch

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    for(int i = 1;i = times;i++)
    That line says that the code in the 'for' loop will only be executed when i = times. Me thinks you want to do

    Code:
    for(int i = 1;i <= times;i++)
    or something like it.

    And you can't do

    Code:
    if(item == "memory card")
    You will need to use strcmp(), so look that one up.

    Hope this helps
    Last edited by minesweeper; 01-14-2003 at 07:00 PM.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i thought for the for() u had it like

    for(i = sth to start with;what i has to equal to stop;and how i changes) so what is it then can run that by me on more time thanx

    and its been awhile i forgot about strcmp thanx for the reminder.
    hooch

  4. #4
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    for(int i = 1;i <= times;i++)
    That means:

    int i = 1;

    Run the loop whilst (i <= times) is TRUE;

    increment (+1) i;

    Once 'i' has incremented to being greater than 'times' the loop will cease to run.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    k ty but why is it giving me those warnings? the other float declarations are working fine so what is wrong with the first two and i checked my book it saids i can do it that way cant i?
    hooch

  6. #6
    The main problem I see is that you didn't use code tags

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i have no idea what that is if i ever kne i forgot sorry
    hooch

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    code tags: http://cboard.cprogramming.com/showt...threadid=13473

    have your tried to use double instead of float, it may help.

  9. #9
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Don't worry, he's just being pedantic



    About your warnings, not entirely sure but no doubt it is because you are converting from one data type to another of different size. And in doing so you are losing some data. In this case most likely the precision of your data will be reduced.

    Someone else may well know for sure....any takers?

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    double can handle decimals? i didnt kno it could?
    hooch

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    yes it could, i believe float was for c, and double was introduced in c++, please correct me if i'm wrong. anyways, i had no errors or warnings with your previous code, but as stated above, there are runtime errors that need to be addressed.

    edit: actually i did get a backwards warning for using old style headers...
    Last edited by alpha; 01-14-2003 at 07:44 PM.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok i will try that but does it work the exact same way i have it i jus inplant double for float? or is the syntax diff?
    hooch

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by alpha
    yes it could, i believe float was for c, and double was introduced in c++, please correct me if i'm wrong.
    No. C has both double and float values. One is simply larger than the other. Much like a 'char' and an 'int' both hold integral values. One is (typically) larger than the other.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    okay, thanks quzah. for some reason, i always thought double wasn't in c.

    anyways, i think it behaves similarly, but also, i would recommend making your values const.

    edit: I haven't learned c, so that is probably why i thought that.

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    here is what i have changed this thing too and the output im getting if this will help and the warnings still havent gone away


    BEGIN CODE
    _________________
    // Memory card = 69.95
    // Software = 34.98

    #include <iostream.h>
    #include <string.h>
    int main()
    {
    double mem_card = (double) 0.00;
    double software = (double) 0.00;
    mem_card = 69.95;
    software = 34.98;
    float nw_pc = 675.00;
    float disk_drive = 198.50;
    float total;
    char item[30];
    int num_of;
    int times;
    /* int memory;
    int soft;
    int nw;
    int disk;*/

    /* cout << "How many Memory cards would you like to purchase? ";
    cin >> memory;
    cout << "How much software would you like to buy? ";
    cin >> soft;
    cout << "How many new pcs would you like to buy? ";
    cin >> nw;
    cout << "How many disk drives would you like to buy? ";
    cin >> disk; */
    cout << "How many items would you like to buy? ";
    cin >> times;
    for(int i = 1;i <= times;i++)
    {
    cout << "Which items would you like to buy(Enter one item at a time then hit enter then type in how many you want to buy)? \n";
    cin.get(item,25);
    cin>>num_of;
    if(strcmp(item,"memory card")==0)
    total = total + (mem_card * num_of);
    else if(strcmp(item,"software")==0)
    total = total + (software * num_of);
    else if(strcmp(item,"new pc")==0)
    total = total + (nw_pc * num_of);
    else if(strcmp(item,"disk drive")==0)
    total = total + (disk_drive * num_of);
    else
    cout << "That is an invalid choice.\n";
    }
    cout << "Your total is " << total << endl;
    return 0;
    }
    /* OUT PUT HERE
    How many items would you like to buy? 3
    Which items would you like to buy(Enter one item at a time then hit enter then t
    ype in how many you want to buy)?
    memory card
    That is an invalid choice.
    Which items would you like to buy(Enter one item at a time then hit enter then t
    ype in how many you want to buy)?
    That is an invalid choice.
    Which items would you like to buy(Enter one item at a time then hit enter then t
    ype in how many you want to buy)?
    That is an invalid choice.
    Your total is -1.07374e+008
    */

    --------------------Configuration: projecthooch - Win32 Debug--------------------
    Compiling...
    projecthooch.cpp
    C:\projecthooch.cpp(39) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    C:\projecthooch.cpp(41) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    Linking...

    __________________
    END CODE

    here that will help

    sorry im normally better at this but its been a while


    aight thanx
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM