Thread: quick question

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

    quick question

    i have a quick question my program goes thro the code normally till it hits where it is supposed to have the second input part and then it totally ignores taht and throws in random numbers? why is this can anyone help me out here?i threw in the dummy because i heard sometimes you have to do that with cin.get() so i wasnt to sure but it didnt do anything so im kind of lost as to what to do?
    Code:
    #include <iostream.h> 
    #include <string.h>
    #include <iomanip.h>
    const double mem_card = 69.95;
    	const double software = 34.98;
    	const double nw_pc = 675.00;
    	const double disk_drive = 198.50;
    
    
    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;*/
    double total = 0;
    	char item[30];
    double num_of;
    double times;
    char dummy[10];
    	/*	int memory;
    	int soft;
    	int nw;
    	int 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.get(dummy,5);
    	cout << "Space filler\n";
    
    	cin>>num_of;
    cout << "number of = " << num_of << endl;
    	if(strcmp(item,"memory card"))
    
    	total = total + (mem_card * num_of);
    else if(strcmp(item,"software")) 
    	total = total + (software * num_of);
    else if(strcmp(item,"new pc"))
    total = total + (nw_pc * num_of);
    else if(strcmp(item,"disk drive"))
    total = total + (disk_drive * num_of);
    else
    cout << "That is an invalid choice.\n";
    
    }
    cout << "Your total is ";
    cout << setprecision(2) << total << endl;
    return 0;
    }

    &#91;code]&#91;/code]tagged by Salem (wondering why ppl with larger post counts still haven't figured it out)
    hooch

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    you can do it like this:

    Code:
    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.getline(item,25,'\n');
    cin.getline(item,25,'\n');
    
    cout << "Space filler\n";

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    the problem lies in the strcmp line, you have "memory card", you have to fix the space. I revised the code and pasted the program as i got it to work, notice the _ to fill the space. I forget how to re-write that strcmp line correctly, but a lil researching on the boards and u'll be fine.

    Also not everything has to be a double, hence your bad spitting numbers problem. num_of can be a int, as no one is going to order 1.5 sticks of memory. Same as times, no one is going to run this loop 1.5 times (not in a day to day situation anyway).

    output:

    Code:
    How many items would you like to buy? 2
    Which item would you like to buy? memory_card
    How many?: 4
    
    number of = 4
    Which item would you like to buy? memory_card
    How many?: 4
    
    number of = 4
    Your total is 559.60
    Press any key to continue
    revised code:

    Code:
    #include <iostream.h> 
    #include <string.h>
    #include <iomanip.h>
    const double mem_card = 69.95;
    const double software = 34.98;
    const double nw_pc = 675.00;
    const double disk_drive = 198.50;
    
    #define FLAGS	cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)<<setprecision(2);
    
    int main()
    {
    double total = 0;
    char item[30];
    int num_of;
    int times;
    /* int memory;
    int soft;
    int nw;
    int disk;*/ 
    
    FLAGS;
    
    cout << "How many items would you like to buy? ";
    cin >> times;
    
    for(int i = 1;i <= times;i++)
    {
    
    
    cout << "Which item would you like to buy? ";
    cin>>item;
    
    cout<< "How many?: ";
    cin>>num_of;
    cout<<"\n";
    
    
    
    
    cout << "number of = " << num_of << endl;
    
    if(strcmp(item,"memory card"))
    
    total = total + (mem_card * num_of);
    else if(strcmp(item,"software")) 
    total = total + (software * num_of);
    else if(strcmp(item,"new pc"))
    total = total + (nw_pc * num_of);
    else if(strcmp(item,"disk drive"))
    total = total + (disk_drive * num_of);
    else
    cout << "That is an invalid choice.\n";
    }
    
    cout << "Your total is ";
    cout << setprecision(2) << total << endl;
    
    
    return 0;
    }
    Last edited by RoD; 01-15-2003 at 11:27 AM.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>if(strcmp(item,"memory card"))
    strcmp returns 0 if there's a match, your tests have a logic error.
    *Cela*

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok ty that helps but y is the output doing this now?
    giving me the weird numbers at the end for the total?

    heres the output

    How many items would you like to buy? 2
    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
    Space filler
    1
    number of = 1
    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)?
    new pc
    Space filler
    1
    number of = 1
    Your total is 1e+002
    hooch

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok ill try that and sorry i didnt see ur post before i posted mine didnt mean to
    hooch

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ie been studying this on my own for a while and i havent done it much lately and now im in school doing and it you lost me with that #define thing you did what does taht do?
    hooch

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    why wont this work with cin.getline(item,25); ? im supposed to be using it in school as this for school and it wont work using? but isnt that the right syntax for? to my knownledge it is unless im wrong?
    hooch

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    [QUOTE]tagged by Salem (wondering why ppl with larger post counts still haven't figured it out)[QUOTE]

    Maybe some sort of module that detects code and if no code tags are present does not allow them to post. If that is at all possible, not allowing them to post instead of just puting code tags on for them might force them to read the dam Sticky posts.

    Just a thought.

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    post your revised code and we'll take a peek.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    sorry lol how about this

    _________

    cin.getline(item,25);


    _________


    why wont that work isnt that the right syntax?

    that better lol?
    hooch

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    k

    BEGIN CODE


    ____________-

    #include <iostream.h>
    #include <string.h>
    #include <iomanip.h>
    const double mem_card = 69.95;
    const double software = 34.98;
    const double nw_pc = 675.00;
    const double disk_drive = 198.50;


    //Confused as to what this actually does but i copied it from earlier but he said it helped?
    #define FLAGS cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)<<setprecision(2);

    int main()
    {
    double total = 0;
    char item[30];
    int num_of;
    int times;
    /* int memory;
    int soft;
    int nw;
    int disk;*/

    FLAGS;

    cout << "How many items would you like to buy? ";
    cin >> times;

    for(int i = 1;i <= times;i++)
    {


    cout << "Which item would you like to buy? ";
    //its completely skipping over this getline

    cin.getline(item,30);

    cout<< "How many?: ";
    cin>>num_of;
    cout<<"\n";




    cout << "number of = " << num_of << endl;

    if(strcmp(item,"memory_card"))

    total = total + (mem_card * num_of);
    else if(strcmp(item,"software"))
    total = total + (software * num_of);
    else if(strcmp(item,"new_pc"))
    total = total + (nw_pc * num_of);
    else if(strcmp(item,"disk_drive"))
    total = total + (disk_drive * num_of);
    else
    cout << "That is an invalid choice.\n";
    }

    cout << "Your total is ";
    cout << setprecision(2) << total << endl;


    return 0;
    }

    _____

    END CODE


    There i added comments on to where im confused so just read those k? thanx
    hooch

  13. #13
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    thats the same as typing

    cout<<setiosflags(ios::fixed | ios::showpoint | ios::right)
    <<setprecision(2);

    in your code to format output, i just put that line into a variable and called it.

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    its skipping it because the previous cin>> call left a newline in the stream.This can be countered by adding this line before the getline...
    cin.ignore(80,'\n');
    that will stop it skipping.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    you mean my prevous version of the code when it had just plain
    ______
    cin>>item;
    ______

    that left a new line? if thats what you mean i somewhat understand otherwise im confused?
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM