Question:

I'm creating a receipt program in C

by  |  earlier

0 LIKES UnLike

I'm creating this C receipt program and i'm trying to loop it using the goto statement but whenever i the program goes back to menu: using the goto statement it seems to bypass the getline statement i used thus it doesn't ask for the string whenever it asks for an order from the menu again, maybe you'll be able to see what's wrong just by looking at the program need tips and advice thanks!

#include <iostream>

#include <string>

using namespace std;

void main ()

{

string product;

char decision;

double price,pcs,total,paid,change=0;

menu:

cout <<"Place Your Order Now: " <<endl;

cout <<"=====================================... <<endl;

cout <<"Enter Product: " ;

getline (cin,product);

cout <<"Enter the Price: P" ;

cin >> price;

cout <<"Enter Number of Pieces: " ;

cin >> pcs;

total = price * pcs;

cout <<"=====================================... <<endl<<endl;

cout <<product;

cout <<endl<<endl;

cout <<"Total Amount Due = P" <<total <<endl;

cout <<"=====================================... <<endl;

cout <<"Enter Payment: P";

cin >> paid;

cout <<"=====================================... <<endl;

change = paid - total;

cout <<"Change = P" << change;

cout <<endl <<endl <<"01 James Lim C example" <<"\n" <<"\n";

cout <<"Would you like to order again? <y> yes <n> no = ";

cin >>decision;

if (decision == 'y' || decision == 'Y')

goto menu;

else if (decision == 'n' || decision == 'N')

goto quit;

else

cout <<"invalid input! \n";

goto menu;

quit:

cout <<"Thank you! Please come again! \n";

}

 Tags:

   Report

3 ANSWERS


  1. Personally, I&#039;d break it up into 3 functions:

    main();

    getProductsAndChange();

    displayResults();

    The reason for this is that the code is easier to read by containing the prompting and validation of what they entered much better within their function scope.

    There are also less variables in the main() function, thereby also making it easier to read.

    As an example, what if you wanted the customer to be able to enter 1 or more products. Then you can see you would edit the &quot;getProductsAndChange()&quot; function but you could still return the list of products in the &quot;product&quot; variable (potentially separated by commas or some other delimiter). And the resultant &quot;change&quot; variable would be set after the total of all the products was tallied.

    This doesn&#039;t cause you to have to change anything in main() or in displayResults().

    Using goto is a bad habit to get into when using procedural languages. It is rarely necessary as you can see.

    void main() {

    BOOL bDone = FALSE;

    string product;

    double change;

       while ( ! bDone) {

          getInput(&amp;product, &amp;change);

          bDone = displayResults(product, change);

       }

    }

    /*

       This function is used to get the initial information, returning the response by passing variables by reference

       Input values:

          product - used to return the chosen product to the function which called this function

          change - used to return the change the customer receives to the function which called this function

       Return values:

          None

    */

    void getProductsAndChange(double &amp;product, double &amp;change) {

       double price,pcs,total,paid;

       count &lt;&lt; &quot;Place Your Order Now: &quot; &lt;&lt;endl;

       cout &lt;&lt;&quot;=====================================...

       cout &lt;&lt;&quot;Enter Product: &quot; ;

       getline (cin,product);

       cout &lt;&lt;&quot;Enter the Price: &quot; ;

       cin &gt;&gt; price;

       cout &lt;&lt;&quot;Enter Number of Pieces: &quot; ;

       cin &gt;&gt; pcs;

       total = price * pcs;

       cout &lt;&lt;&quot;Total Amount Due = &quot; &lt;&lt;total &lt;&lt;endl;

       cout &lt;&lt;&quot;=====================================... &lt;&lt;endl;

       cout &lt;&lt;&quot;Enter Payment: &quot;;

       cin &gt;&gt; paid;

       cout &lt;&lt;&quot;=====================================... &lt;&lt;endl;

       change = paid - total;

    }

    /*

       This function is used to display the results and ask if the customer wants to order again

       Return values:

          TRUE - if and only if the customer wants to continue

          FALSE - otherwise

    */

    BOOL displayResults(double product, double change) {

       char decision;

       BOOL bReturn;

       BOOL bValidDecision = false;

       cout &lt;&lt;&quot;=====================================... &lt;&lt;endl&lt;&lt;endl;

       cout &lt;&lt;product;

       cout &lt;&lt;endl&lt;&lt;endl;

       cout &lt;&lt;&quot;Change = &quot; &lt;&lt; change;

       cout &lt;&lt;endl &lt;&lt;endl &lt;&lt;&quot;01 James Lim C example&quot; &lt;&lt;&quot;\n&quot; &lt;&lt;&quot;\n&quot;;

       cout &lt;&lt;&quot;Would you like to order again? &lt;y&gt; yes &lt;n&gt; no = &quot;;

       cin &gt;&gt;decision;

       while (! bValidDecision ) {

          switch (decision)

          {

             case &#039;y&#039;:

             case &#039;Y&#039;:

             case &#039;1&#039;:

                bReturn = TRUE;

    bValidDecision = TRUE;

                break;

             case &#039;n&#039;:

             case &#039;N&#039;:

             case &#039;0&#039;:

                bReturn = FALSE;

    bValidDecision = TRUE;

                break;

             default:

            cout &lt;&lt;&quot;invalid input! \n&quot;;

                break;

          }

       }

       return bReturn;

    }


  2. yes i have experienced same thing a lots of time, basically you need to flush the iostream buffer.

    But my suggestion is to use &quot;do - while&quot; loop construct

    do

    {

    cout &lt;&lt;&quot;Place Your Order Now: &quot; &lt;&lt;endl;

    // snip

              //other code remains same

    // snip

    cout &lt;&lt;&quot;Would you like to order again? &lt;y&gt; yes &lt;n&gt; no = &quot;;

    cin &gt;&gt;decision;

    } while (decision !=&#039;n&#039; || decision!=&#039;N&#039;);

  3. Instead of using that goto statement and Tags u can use do while loop

    void main ()

    {

    string product;

    char decision;

    double price,pcs,total,paid,change=0;

    do

    {

    cout &lt;&lt;&quot;Place Your Order Now: &quot; &lt;&lt;endl;

    cout &lt;&lt;&quot;=====================================... &lt;&lt;endl;

    cout &lt;&lt;&quot;Enter Product: &quot; ;

    getline (cin,product);

    cout &lt;&lt;&quot;Enter the Price: P&quot; ;

    cin &gt;&gt; price;

    cout &lt;&lt;&quot;Enter Number of Pieces: &quot; ;

    cin &gt;&gt; pcs;

    total = price * pcs;

    cout &lt;&lt;&quot;=====================================... &lt;&lt;endl&lt;&lt;endl;

    cout &lt;&lt;product;

    cout &lt;&lt;endl&lt;&lt;endl;

    cout &lt;&lt;&quot;Total Amount Due = P&quot; &lt;&lt;total &lt;&lt;endl;

    cout &lt;&lt;&quot;=====================================... &lt;&lt;endl;

    cout &lt;&lt;&quot;Enter Payment: P&quot;;

    cin &gt;&gt; paid;

    cout &lt;&lt;&quot;=====================================... &lt;&lt;endl;

    change = paid - total;

    cout &lt;&lt;&quot;Change = P&quot; &lt;&lt; change;

    cout &lt;&lt;endl &lt;&lt;endl &lt;&lt;&quot;01 James Lim C example&quot; &lt;&lt;&quot;\n&quot; &lt;&lt;&quot;\n&quot;;

    cout &lt;&lt;&quot;Would you like to order again? &lt;y&gt; yes &lt;n&gt; no = &quot;;

    cin &gt;&gt;decision;

    }

    while(decision == &#039;y&#039; || decision == &#039;Y&#039;);

    if (!(decision == &#039;n&#039; || decision == &#039;N&#039;))

        cout &lt;&lt;&quot;invalid input! \n Closing Application\n&quot;;

    cout &lt;&lt;&quot;Thank you! Please come again! \n&quot;;

    }

    You might need to format it a bit.But this sure will help

    Dude i gotta ask you something too

    Which tool you are using for editing and compiling

    Pleeeeeeeease reply me on rizvanmotala99@yahoo.co.in

    Thanks

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.