CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    Need Help In Completing this code

    I am having some trouble with this code that I am trying to complete.
    I know I should be adding a loop or something somewhere but I dont
    know where. Also I am very new at programming and it is my first
    time using switches. But it is not working ( I think ).
    Could you be kind enough to guide me in the right direction. It would
    be a great help to me.
    I thank you in advance.

    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    main()
    {
    clrscr();
    int day,Pnumber=0,Usold=0,count1=0,count2=0,count3=0,P1=0,P2=0,P3=0;

    cout<<"\nPress ctrl + z to end program.";
    cout<<"\n\nThe day of the week should be in Numbers.";
    cout<<"\neg: Monday = 1, Tuesday = 2 and so on.";

    cout<<"\n\nEnter the Day of the Week:"; // Day of week is a number
    cin>>day; // eg: Monday = 1, Tuesday = 2 and so on
    cout<<"\nEnter the Part Number:";
    cin>>Pnumber; // Part numbers are 1 , 2 or 3 only
    cout<<"\nEnter Units Sold:";
    cin>>Usold; // How many Units were sold

    while (( Pnumber = getchar() ) !=EOF )
    {

    switch (Pnumber) //P1,P2,P3 will hold the subtotal
    { //in the end for the output
    case 1:
    int P1 = 0;
    P1 = (Pnumber * 1);
    int count1 = 0; //count1,2,3 will hold the output
    count1 = (Usold + count1); //for Units sold
    break;

    case 2:
    int P2 = 0;
    P2 = (Pnumber * 2);
    int count2 = 0;
    count2 = (P2 + count2);
    break;

    case 3:
    int P3 = 0;
    P3 = (Pnumber * 3);
    int count3 = 0;
    count3 = (P3 + count3);
    break;

    default:
    cout<<"\n\nWrong Part Number Used";
    cout<<"\nPlease use only Part Numbers 1 or 2 or 3";
    break;
    }
    }
    if ( day == 1 )
    cout<<"Monday";
    if ( day == 2 )
    cout<<"Tuesday";
    if ( day == 3 )
    cout<<"Wednesday";
    if ( day == 4 )
    cout<<"Thursday";
    if ( day == 5 )
    cout<<"Friday";
    if ( day == 6 )
    cout<<"Saturday";
    if ( day == 7 )
    cout<<"Sunday";

    cout<<"\nPart Number Unit Price Units Sold Sub Total";
    cout<<"\n1"<<"\t\t$1"<<"\t\t"<<count1<<"\t"<<"\t"<<P1;
    cout<<"\n2"<<"\t\t$2"<<"\t\t"<<count2<<"\t"<<"\t"<<P2;
    cout<<"\n3"<<"\t\t$3"<<"\t\t"<<count3<<"\t"<<"\t"<<P3;
    getch();
    return 0;
    }


  2. #2
    Guest

    Re: Need Help In Completing this code

    Hi!

    The getchar() function probably returns the ascii value of the key read, which for the '1' is 49. So what you probalby need to do is subtract 48 from Pnumber and then do the switch command.

    I'm a beginner myself and to me this seems like the most likely solution to your problem.

    Regards,
    Marcel


  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: Need Help In Completing this code

    OK, firstly you are declaring variables P1, P2, P3, count1, count2, and count3 twice, once at the beginning, and again inside the 'case' blocks in the switch statement. The ones in the 'case' blocks hide the previously declared ones, and
    only exist until the end of the 'case' block, so none of the code using P1, P2, P3, count1, count2, and count3 inside the 'case' blocks has any effect. Remove the declarations from inside the 'case' blocks.

    When you mix iostreams and standard I/O functions, you will find you need to flush the output stream to display the output before any standard I/O input, and flush the standard input before using getchar().

    The getchar() standard I/O function returns the ASCII character value as an int. You need to convert this to the numeric value it represents. A quick and dirty way is to subtract 48. A better solution would be to use cin to input the Part Number, and use the value 0 (zero) to terminate data entry. This would avoid all the flushing.

    You need to prompt for the Units Sold inside the loop so each prompt for a part number is paired with a prompt for Units Sold.

    I couldn't make sense of what you were doing with the P1, P2, and P3 subtotals in the 'case' blocks, so I just assumed they are the total value of each Part Number sold, in which case they don't need to be in the 'case' blocks at all. Total value = total units sold x price. This can be done together with the summary output, at the end of the program.

    Rather than use the '\n' escape character for new lines, it's generally better to use the 'endl' manipulator, which outputs a new line and flushes the output buffer.

    You should avoid putting 'raw' number constants into the body of your code (known as 'magic' numbers in the trade, because nobody can tell what they mean), and use declared variables instead. This way you can give your number a name, and there's no penalty because the compiler handles them the same way.

    Here's my version using getchar(), ASCII conversion and all the stream flushing:

    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    main()
    {
    // clrscr(); // Not on my system
    int day = 0; // Day of week (1-7)
    int Pnumber = 0; // Part number (1-3)
    int Usold = 0; // Units sold
    int count1 = 0; // Total units for Part No.1
    int count2 = 0; // Total units for Part No.2
    int count3 = 0; // Total units for Part No.3
    const int priceP1 = 1; // Price for Part No.1
    const int priceP2 = 2; // Price for Part No.2
    const int priceP3 = 3; // Price for Part No.3
    const int asciiToNum = 48; // Subtract from ASCII char to get number

    cout << endl;
    cout << "Enter ctrl + z to end data entry." << endl;
    cout << endl;
    cout << "The day of the week should be numeric." << endl;
    cout << "e.g. Monday = 1, Tuesday = 2, and so on." << endl;
    cout << endl;

    // Keep prompting until valid day entered
    while (day < 1 || day > 7)
    {
    cout << "Enter the Day of the Week (1-7): "; // Day of week is a number
    cin >> day; // eg: Monday = 1, Tuesday = 2 and so on
    }

    cout << endl;
    cout << "Enter the Part Number:\t";
    cout.flush(); // Flush output stream to display output

    while (( Pnumber = getchar() ) !=EOF )
    {
    Pnumber -= asciiToNum; // Convert ASCII to number

    cout << "Enter the Units Sold:\t";
    cin >> Usold; // How many Units were sold

    switch (Pnumber)
    {
    case 1:
    count1 += Usold; //for Units sold
    break;

    case 2:
    count2 += Usold;
    break;

    case 3:
    count3 += Usold;
    break;

    default:
    cout << endl << endl << "Wrong Part Number Used" << endl;
    cout << "Please use only Part Numbers 1 or 2 or 3" << endl;
    break;
    }
    cout << endl << "Enter the Part Number:\t";
    cout.flush(); // Flush output stream to display output
    fflush(stdin); // Flush input stream to prepare for intput
    }

    cout << endl;

    switch (day)
    {
    case 1:
    cout << "Monday";
    break;
    case 2:
    cout << "Tuesday";
    break;
    case 3:
    cout << "Wednesday";
    break;
    case 4:
    cout << "Thursday";
    break;
    case 5:
    cout << "Friday";
    break;
    case 6:
    cout << "Saturday";
    break;
    case 7:
    cout << "Sunday";
    break;
    }
    cout << endl << endl;

    cout << "Part No.\tUnit Price\tUnits Sold\tSub Total" << endl;
    cout << "1" << "\t\t" << "$1" << "\t\t" << count1 << "\t\t$" << (priceP1 * count1) << endl;
    cout << "2" << "\t\t" << "$2" << "\t\t" << count2 << "\t\t$" << (priceP2 * count2) << endl;
    cout << "3" << "\t\t" << "$3" << "\t\t" << count3 << "\t\t$" << (priceP3 * count3) << endl;
    getch();
    return 0;
    }

    Here's my version using cin >> Pnumber instead of getchar(). This makes it a bit simpler and clearer:

    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    main()
    {
    // clrscr();
    int day = 0; // Day of week (1-7)
    int Pnumber = 0; // Part number (1-3)
    int Usold = 0; // Units sold
    int count1 = 0; // Total units for Part No.1
    int count2 = 0; // Total units for Part No.2
    int count3 = 0; // Total units for Part No.3
    int priceP1 = 1; // Price for Part No.1
    int priceP2 = 2; // Price for Part No.2
    int priceP3 = 3; // Price for Part No.3

    cout << endl;
    cout << "Enter 0 for Part Number to end data entry." << endl;
    cout << endl;
    cout << "The day of the week should be numeric." << endl;
    cout << "e.g. Monday = 1, Tuesday = 2, and so on." << endl;
    cout << endl;

    // Keep prompting until valid day entered
    while (day < 1 || day > 7)
    {
    cout << "Enter the Day of the Week (1-7): "; // Day of week is a number
    cin >> day; // eg: Monday = 1, Tuesday = 2 and so on
    }

    cout << endl;
    cout << "Enter the Part Number:\t";

    cin >> Pnumber;
    while (Pnumber != 0)
    {
    cout << "Enter the Units Sold:\t";
    cin >> Usold; // How many Units were sold

    switch (Pnumber)
    {
    case 1:
    count1 += Usold; //for Units sold
    break;

    case 2:
    count2 += Usold;
    break;

    case 3:
    count3 += Usold;
    break;

    default:
    cout << endl << endl << "Wrong Part Number Used" << endl;
    cout << "Please use only Part Numbers 1 or 2 or 3" << endl;
    break;
    }
    cout << endl << "Enter the Part Number:\t";
    cin >> Pnumber;
    }

    cout << endl;

    switch (day)
    {
    case 1:
    cout << "Monday";
    break;
    case 2:
    cout << "Tuesday";
    break;
    case 3:
    cout << "Wednesday";
    break;
    case 4:
    cout << "Thursday";
    break;
    case 5:
    cout << "Friday";
    break;
    case 6:
    cout << "Saturday";
    break;
    case 7:
    cout << "Sunday";
    break;
    }
    cout << endl << endl;

    cout << "Part No.\tUnit Price\tUnits Sold\tSub Total" << endl;
    cout << "1" << "\t\t" << "$1" << "\t\t" << count1 << "\t\t$" << (priceP1 * count1) << endl;
    cout << "2" << "\t\t" << "$2" << "\t\t" << count2 << "\t\t$" << (priceP2 * count2) << endl;
    cout << "3" << "\t\t" << "$3" << "\t\t" << count3 << "\t\t$" << (priceP3 * count3) << endl;
    getch();
    return 0;
    }

    Compare my versions with your version to see exactly where the changes are.

    If there's anything here you don't understand, let me know.

    I hope this helps.

    Dave


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured