Need Help!

Discussion in 'C++' started by bryant058@gmail.com, Dec 5, 2006.

  1. Guest

    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;
    }
    , Dec 5, 2006
    #1
    1. Advertising

  2. * :
    > 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?

    --
    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?
    Alf P. Steinbach, Dec 5, 2006
    #2
    1. Advertising

  3. Guest

    Alf P. Steinbach wrote:
    > * :
    > > 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?
    >
    > --
    > 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.
    , Dec 5, 2006
    #3
  4. Guest

    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
    > > Enter Product Number:49 // number
    > > Enter Quantity Number:2
    > > Enter Product Number:-1


    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.
    , Dec 5, 2006
    #4
  5. Guest

    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;
    }
    , Dec 5, 2006
    #5
  6. Guest

    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;
    }
    , Dec 5, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,447
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    752
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    380
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    537
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    470
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page