-
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;
}
[code][/code]tagged by Salem (wondering why ppl with larger post counts still haven't figured it out)
-
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";
-
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;
}
-
>>if(strcmp(item,"memory card"))
strcmp returns 0 if there's a match, your tests have a logic error.
-
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
-
ok ill try that and sorry i didnt see ur post before i posted mine didnt mean to
-
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?
-
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?
-
[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.
-
post your revised code and we'll take a peek.
-
sorry lol how about this
_________
cin.getline(item,25);
_________
why wont that work isnt that the right syntax?
that better lol?
-
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
-
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.
-
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.
-
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?