Need Help!

B

bryant058

i write a program,but after test ,the cout is still 0 !! Why?
----My source code-------------
#include<iostream>
using namespace std;
int main()
{
int pn,qn;
double total=0;
cout<<"Enter Product Number:";
cin>>pn;
while(pn != -1)
{
cout<<"Enter Quantity Number:";
cin>>qn;
switch (pn)
{
case'1':
total=20;
break;

case'2':
total=total+qn*4.50;
break;

case'3':
total=total+qn*9.98;
break;

case'4':
total=total+qn*4.49;
break;

case'5':
total=total+qn*6.87;
break;

}
cout<<"Enter Product Number:";
cin>>pn;
}
cout<<total<<endl;

return 0;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
i write a program,but after test ,the cout is still 0 !! Why?
----My source code-------------
#include<iostream>
using namespace std;
int main()
{
int pn,qn;
double total=0;
cout<<"Enter Product Number:";
cin>>pn;
while(pn != -1)
{
cout<<"Enter Quantity Number:";
cin>>qn;
switch (pn)
{
case'1':
total=20;
break;

case'2':
total=total+qn*4.50;
break;

case'3':
total=total+qn*9.98;
break;

case'4':
total=total+qn*4.49;
break;

case'5':
total=total+qn*6.87;
break;

}
cout<<"Enter Product Number:";
cin>>pn;
}
cout<<total<<endl;

return 0;
}

It works fine for me:

V:\> a
Enter Product Number:49
Enter Quantity Number:2
Enter Product Number:-1
20

V:\>

How did you test?
 
C

chengsshi

Alf said:
* (e-mail address removed):

It works fine for me:

V:\> a
Enter Product Number:49
Enter Quantity Number:2
Enter Product Number:-1
20

V:\>

How did you test?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

the pn only can be '1','2' '3','4','5', so the 49 is wrong, (i think
you should a default here , lile this:
default:
cout << "the pn number is wrong, please try again..." <<endl;)
i think the reason is the total can not pass through switch, but why
pn is 49, it can passed 20, i did not know. maybe i need to look it
again.
 
C

chengsshi

I got it, (sorry, the default have a little wrong),
case '1' ,case '2',case '3' ,case '4', case '5' , the number in this
character.

but in the
so if you want to got '1', you should write 49 (0x31), '2'
is50(0x32); and so on.

attention: the character and number is different.
 
C

chengsshi

the correct one:

#include<iostream>
using namespace std;

int main()
{
int pn, qn;
double total = 0;
cout << "Enter Product Number:";
cin >> pn;
while (pn != -1)
{
cout << "Enter Quantity Number:";
cin >> qn;
switch (pn)
{
case 1:
total=20;
break;
case 2:
total=total+qn*4.50;
break;
case 3:
total=total+qn*9.98;
break;
case 4:
total=total+qn*4.49;
break;
case 5:
total=total+qn*6.87;
break;
default:
cout << "the pn number is wrong, please try again..." <<endl;
}
cout << "Enter Product Number:";
cin >> pn;
}
cout << total << endl;
system("pause");
return 0;
}
 
C

chengsshi

the correct:


#include<iostream>
using namespace std;

int main()
{
int pn, qn;
double total = 0;
cout << "Enter Product Number:";
cin >> pn;
while (pn != -1)
{
cout << "Enter Quantity Number:";
cin >> qn;
switch (pn)
{
case 1:
total=20;
break;
case 2:
total=total+qn*4.50;
break;
case 3:
total=total+qn*9.98;
break;
case 4:
total=total+qn*4.49;
break;
case 5:
total=total+qn*6.87;
break;
default:
cout << "the pn number is wrong, please try again..." <<endl;
}
cout << "Enter Product Number:";
cin >> pn;
}
cout << total << endl;
system("pause");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top