Link to home
Start Free TrialLog in
Avatar of seraph_matrix_631
seraph_matrix_631Flag for United Kingdom of Great Britain and Northern Ireland

asked on

C++ program, Added IF statments and conditions

***  CODE START ***

/*
REFECTORY INTERVIEW HEADER
- - - - - - ** - - - - - -

AUTHOR                  BEN LACEY [20026111]
COPYRIGHT            DECEMBER 2007
CREATED                  06/12/2007  11:55 AM
LAST UPDATED      14/12/2007      03:04 PM
*/


#include<iostream.h>
#include<stdlib.h>
void main()
{// Start Main Program


      //Variables Used In Program
      int nCounter=0;                  //INITIALISE nCounter with 0
      char chRefecAns=0;            //INITIALISE chRefecAns with 0
      char chServAns=0;            //INITIALISE chServAns with 0
      int nRefecCount=0;            //INITIALISE nRefecCount with 0
      int nServHappyCount=0;      //INITIALIZE nServCount with 0
      double nServPercent=0;      //INITIALIZE nServPercent with 0
      double nRefecPercent=0;      //INITIALIZE nRefecPercent with 0
      
      //Counter Values For Food Selection
      int nSandwichCount=0;            
      int nSaladCount=0;
      int nPotatoCount=0;
      int nSnackCount=0;
      int nMealCount=0;
      int nSelection=0;            // Used in the SWITCH statment

      

      for(nCounter=0; nCounter<10; nCounter++)      //Set the program to loop x amount of times
            {// Start The Counted Program Loop

            // Ask Question 1
            cout << "Do you use the refectory regularly?" << endl;
            cout << "Enter Y (for yes) or N (for no): ";
            cin >> chRefecAns;
            cout << endl;

            if ((chRefecAns == 'Y') || (chRefecAns == 'y')) //Make sure user types a Y or y
            {//Validate Answer For Question 1, If YES Run The Code Below This Line
                  //Update the refectory usage counter if Question 1 = Yes
                  nRefecCount++; //This is the count of the people who said YES to Question 1
                  

                  // Ask Question 2
                  cout << "Are you happy with the service provided?" << endl;
                  cout << "Enter Y (for yes) or N (for no): ";
                  cin >> chServAns;
                  cout << endl;
            
                  //Update the Service Happy Counter if Question 2 = Yes
                  if ((chServAns == 'Y') || (chServAns == 'y'))
                  {
                        nServHappyCount++;      //Increment Counter for Service Users
                  }// End Second Question
            }//End Validation for Question 1, Question 2, and Question 2 Validation


            cout << endl << endl;
            cout << "Please tell us what you bought" << endl;
            cout << "Enter the appropiate number (1)-(5):" << endl;
            cout << "  (1)   a sandwich" << endl;
            cout << "  (2)   a salad" << endl;
            cout << "  (3)   a baked potato" << endl;
            cout << "  (4)   a hot snack" << endl;
            cout << "  (5)   a full meal" << endl;
            cout << endl;
            cout << "Selection: ";
            cin >> nSelection;


                  switch(nSelection)
                  {
                        case 1: nSandwichCount++; system("cls"); break;
                        case 2: nSaladCount++; system("cls"); break;
                        case 3: nPotatoCount++; system("cls"); break;
                         case 4: nSnackCount++; system("cls"); break;
                        case 5: nMealCount++; system("cls"); break;
                  }
      }// End Counted Program Loop

      

      // When 300 Students have been asked output the results
      nRefecPercent=((float)nRefecCount / (float)nCounter) * 100;
      nServPercent=((float)nServHappyCount / (float)nCounter) *100;
      cout << endl;


      //End Report Shown to Users when all students have been interviewed
      cout << "of " << nCounter << " students interviewed:" << endl;
      cout << nRefecPercent << "% are regular users" << endl;
      cout << "of these:" << endl;
      cout << nServPercent << "% are happy with the service" << endl;
      cout << endl << endl;

      
      //Purchase Information
      cout << "Of the " << nCounter << " refectory users interviwed, the purchases were:" << endl;
      cout << "  " << nSandwichCount << "   bought a sandwich" << endl;
      cout << "  " << nSaladCount << "   bought a salad" << endl;
      cout << "  " << nPotatoCount << "   bought a baked potato" << endl;
      cout << "  " << nSnackCount << "   bought a hot snack" << endl;
      cout << "  " << nMealCount << "   bought a full meal" << endl;
      cout <<      endl;
}// End Main Program

*** CODE END ***


I have a problem with the above code. What i need to do is as follows:-
1. i need to accept Y, y, N, n, X, x for the first prompt.  for question 2 you can have the responces "Y, y, N, n"

if anything different then have "unrecognised input - please re-enter" be shown on screen.


2. with the food i need it to show the message "unrecognised food choice - please re-enter"
     if anything other that 1,2, 3, 4, 5


if for question 1 they answer N or n it will bypass question 2 and ask for a food item.



what i have tried so far:-
*********
for(nCounter=0; nCounter<10; nCounter++)      //Set the program to loop x amount of times
      { // Start The Counted Program Loop

            // Ask Question 1
            do
            {
              cout << "Do you use the refectory regularly?" << endl;
            cout << "Enter Y (for yes), N (for no) or X (to quit): ";
              cin >> chRefecAns;
            cout << endl;
            }
            while (!(chRefecAns=='Y') && (chRefecAns=='y') && (chRefecAns=='N') && (chRefecAns=='n') && (chRefecAns=='X') && (chRefecAns=='x'));
********


This does not work as needed, and i have done this for question 2 also. i keep getting confused with all the brackets and im new to C++. All other online references are confusing me further.
Avatar of jkr
jkr
Flag of Germany image

You'd better try something like
            // Ask Question 1
            bool bLoop = true;
            while(1)
            {
              cout << "Do you use the refectory regularly?" << endl;
              cout << "Enter Y (for yes), N (for no) or X (to quit): ";
              cin >> chRefecAns;
              cout << endl;
 
              switch(chRefecAns) 
              {
                case 'Y':
                    // evaluate as "Yes"
                    break;
                case 'N':
                    // evaluate as "No"
                    break;
                case 'X':
                    bLoop = false; // stop loop
                    break;              
              }
            }
            

Open in new window

Avatar of seraph_matrix_631

ASKER

why do you need the:-
     bool bLoop = true;
     while (l)

sorry im seriously new to C++ but i see your login in using a Case statment ....

but would this allow me to:-
1. select no in question 1 and then be taken to the menu for food, without having to repeat code?
2. loop from question 1 to question2 (if answer from Q1 = True) and then go through to the food options
3. quit at the first question and go straight to the end report screen?


thanks for swift reply
Sorry, that should have been

            bool bLoop = true;
            while(bLoop)

... typo :-(

And yes, if you fill in the gaps that read

                    // evaluate as "XYZ"

it will - you can set up other condition variables that will allow you to control the flow of execution depending on the input.
BTW, sorry for not being more precise, but there is the phrase

----------------------------->8------------------------

The Experts won't do your homework for you; it is considered "academic dishonesty" by the Membership Agreement. If you have homework, and don't understand part of it, be honest about it.

----------------------------->8------------------------


in https://www.experts-exchange.com/Community_Support/help.jsp
JKR
this is not homework. I work in a software development industry, I am new to C++ and have been told by my manager that i am to carry on and finish this program for the catering department as the lead developer is off sick and wont be returning to complete this...

and i have been wracking my brains out on this for ages (approx a week) trying to do it, and i thought the experts can help. sorry if i did not make this clear. I do have some C++ programming knowledge (as shown by original post) but was not sure how exactly i should go about doing what my manager has asked of me. If the lead developer had not been ill i would not have been given the assignment and hence would not be in this current situation.

Thank you however for the swift reply and from what i can see a bulletproof method to solve my problem, but with the lack of sleep i have had i will need to sleep and  try it all out when i am clear in thought.

--Seraph--
>>this is not homework

Sorry, but at first glance, it could have been. It is unusual to see no GUIs involved in user input nowadays.
>>>> #include<iostream.h>
Replace that by

#include <iostream>
using namespace std;

The iostream.h is a deprecated header. You should use the headers and classes from C++ standard.

>>>> void main()

It should be

    int main()

for C++ programs. The return value of your prog can be checked on the command line . A return value of 0 signals 'success'.

>>>> while (!(chRefecAns=='Y') && (chRefecAns=='y') && (chRefecAns=='N') && (chRefecAns=='n') && 
>>>> (chRefecAns=='X') && (chRefecAns=='x'));

The while condition is wrong. Better do:

do {
    ...
    chRefecAns = toupper(chRefecAns);  // make upper case
    }
    while (chRefecAns != 'Y' && chRefecAns != 'N' && chRefecAns != 'X');

Regards, Alex
*** code start ***
#include<iostream.h>
#include<stdlib.h>
void main()
{// Start Main Program
      //Variables Used In Program
      int nCounter=0;                  //INITIALISE nCounter with 0
      char chRefecAns=0;            //INITIALISE chRefecAns with 0
      char chServAns=0;            //INITIALISE chServAns with 0
      int nRefecCount=0;            //INITIALISE nRefecCount with 0
      int nServHappyCount=0;      //INITIALIZE nServCount with 0
      double nServPercent=0;      //INITIALIZE nServPercent with 0
      double nRefecPercent=0;      //INITIALIZE nRefecPercent with 0
      
      //Counter Values For Food Selection
      int nSandwichCount=0;            
      int nSaladCount=0;
      int nPotatoCount=0;
      int nSnackCount=0;
      int nMealCount=0;
      int nSelection=0;            // Used in the SWITCH statment

      for(nCounter=0; nCounter<10; nCounter++)      //Set the program to loop x amount of times
      { // Start The Counted Program Loop
            do
            { //begin Q1
              cout << "Do you use the refectory regularly?" << endl;
                cout << "Enter Y (for yes), N (for no) or X (to quit): ";
              cin >> chRefecAns;
                cout << endl;
              chRefecAns = toupper(chRefecAns);  // make upper case
            }
            while (chRefecAns != 'Y' && chRefecAns != 'N' && chRefecAns != 'X');

            switch(chRefecAns)
            { //start switch
               case 'Y':
                        // Ask Question 2
                            nRefecCount++;
                        do
                        {
                           cout << "Are you happy with the service provided?" << endl;
                           cout << "Enter Y (for yes) or N (for no): ";
                           cin >> chServAns;
                           cout << endl;
                               chServAns = toupper(chRefecAns);  // make upper case
                        }
                        while (chRefecAns != 'Y' && chRefecAns != 'N');


                        //Update the Service Happy Counter if Question 2 = Yes
                        if ((chServAns == 'Y') || (chServAns == 'y'))
                        {
                              nServHappyCount++;      //Increment Counter for Service Users
                        } // End Second Question
                        
           case 'N':
                     cout << endl << endl;
                     cout << "Please tell us what you bought" << endl;
                     cout << "Enter the appropiate number (1)-(5):" << endl;
                     cout << "  (1)   a sandwich" << endl;
                     cout << "  (2)   a salad" << endl;
                     cout << "  (3)   a baked potato" << endl;
                     cout << "  (4)   a hot snack" << endl;
                     cout << "  (5)   a full meal" << endl;
                     cout << endl;
                     cout << "Selection: ";
                     cin >> nSelection;

                              switch(nSelection)
                              {
                                    case 1: nSandwichCount++; system("cls"); break;
                                    case 2: nSaladCount++; system("cls"); break;
                                    case 3: nPotatoCount++; system("cls"); break;
                                     case 4: nSnackCount++; system("cls"); break;
                                    case 5: nMealCount++; system("cls"); break;
                              }
                     break;


               case 'X':
                        // When 300 Students have been asked output the results
                        nRefecPercent=((float)nRefecCount / (float)nCounter) * 100;
                        nServPercent=((float)nServHappyCount / (float)nCounter) *100;
                        cout << endl;

                        //End Report Shown to Users when all students have been interviewed
                        cout << "of " << nCounter << " students interviewed:" << endl;
                        cout << nRefecPercent << "% are regular users" << endl;
                        cout << "of these:" << endl;
                        cout << nServPercent << "% are happy with the service" << endl;
                        cout << endl << endl;

                        //Purchase Information
                        cout << "Of the " << nCounter << " refectory users interviwed, the purchases were:" << endl;
                        cout << "  " << nSandwichCount << "   bought a sandwich" << endl;
                        cout << "  " << nSaladCount << "   bought a salad" << endl;
                        cout << "  " << nPotatoCount << "   bought a baked potato" << endl;
                        cout << "  " << nSnackCount << "   bought a hot snack" << endl;
                        cout << "  " << nMealCount << "   bought a full meal" << endl;
                        cout <<      endl;
                        break;
            } //end switch
      } // end counted loop
} //end VOID MAIN
*** cODE eND ***


Tried this ... and it works to some extent. when the user types a X into the first question, it shows the end report, but also shows  the FIRST QUESTION wanting a user response.

i tried setting the bLoop as a variable and setting it to TRUE
then having while(bLoop)   in the question 1 section and it looped Q1
tried the bLoop just before the question 1 and it didnt accept any value for the Y/N/X
>>>>             case 'Y':
You forgot the break for that case, what may explain some issues.
yes i know i did that
i did it because when i type an answer for question 1 (Case: Y) i want it to go through to the menu select regardless of answer.

is there an easy way to stop the loop when x is pressed?
i tried what JKR said above and it didnt work. Came up with a continuous loop of the first question or when it didnt do that it then didnt accept Y, N, or X for question 1
I think the second questions should be asked in a loop as well. A 'x' entered as response for a second question should break the loop and show the first menu.

You might consider to using functions for your menus, calulation and output.

char askRegularly();
char askHappy();
char askPurchase();

double calculatePercentage(int nCounter, int nPartCount);
void showRegularlyAndHappy(int nCounter, int nRefecCount, int nServHappyCount);
void showPurchases(int nCounter, int nSandwichCount, int nSaladCount,
                                  int nPotatoCount, int nSnackCount, int nMealCount);

int main()
{
    //Variables Used In Program
      int nCounter=0;                  //INITIALISE nCounter with 0
      int nRefecCount=0;            //INITIALISE nRefecCount with 0
      int nServHappyCount=0;      //INITIALIZE nServCount with 0
     
      //Counter Values For Food Selection
      int nSandwichCount=0;            
      int nSaladCount=0;
      int nPotatoCount=0;
      int nSnackCount=0;
      int nMealCount=0;
     
     while (true)
     {
          char choice = askRegularly();
          if (choice == 'X')
               break;
          if (choice == 'Y')
          {
                 nRefecCount++;
                 choice = askHappy();
                 if (choice == 'Y')
                       nServHappyCount++;
          }
          static int* counts[5] = { &nSandwichCount, &nSaladCount,
                                                 &nPotatoCount, &nSnackCount, &nMealCount, };
          while (true)
          {
               choice = askPurchase();
               if (choice < '1' || choice > '5')
                      break;        
               (*counts[choice - '1'])++;   // increment counter
               system("cls");        
          }
     }
     if (nCounter == 0)
          return 1;  // no input
     showRegularlyAndHappy(nCounter, nRefecCount, nServHappyCount);
     showPurchases(nCounter, nSandwichCount, nSaladCount,
                               nPotatoCount, nSnackCount, nMealCount);
      return 0;
}

 
*looks at code and is petrified*
... ... ....

I'm so confused with your code!!! lol
im trying to make this as simple as possible, its working up until the loop but is then prompting question 1.

any SIMPLE way to end the loop when X is pressed and the final menu is shown.


ALSO

when i runt he program 10 times the End menu (AKA: Case X) should be displayed and its not
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
brilliant!!
thank you so much for your assistance in this question. all of you have provided a basis to improve my knowledge in C++.

Thank you for your help in this
500 points gladly awarded to:     itsmeandnobodyelse

Very quick responces, very good answers, and the system works as intended so thank you.
Merry Christmas!

All the answers given were of excellent learning value.
My understanding of how C++ works is slowly being improved thanls for the experts on this forum, especially thanks to itsmeandnobodyelse.

ooh!

one last thing...
how would i get visual output (error message) to the user if an incorrect value is entered into question 1 or 2?


*** my attempt ***
            do
            { //begin Question 1
              cout << "Do you use the refectory regularly?" << endl;
            cout << "Enter Y (for yes), N (for no) or X (to quit): ";
              cin >> chRefecAns;
            cout << endl;
              chRefecAns = toupper(chRefecAns);  // make upper case
            

            //      if (!(chRefecAns=='y') || (chRefecAns=='n') || (chRefecAns=='x'))
            //      {
            //      cout << "    Unrecognised Input - Please Re-Enter" << endl << endl;
            //      }
            }
            while (chRefecAns != 'Y' && chRefecAns != 'N' && chRefecAns != 'X');
*** end my attempt ***
>>>> how would i get visual output (error message) to the user
>>>> if an incorrect value is entered into question 1 or 2?

           while (true)
            { //begin Q1
              cout << "Do you use the refectory regularly?" << endl;
                cout << "Enter Y (for yes), N (for no) or X (to quit): ";
              cin >> chRefecAns;
                cout << endl;
              chRefecAns = toupper(chRefecAns);  // make upper case
              if (chRefecAns == 'Y' || chRefecAns == 'N' || chRefecAns == 'X')
                   break;   // break actively if answer is good
              cout << "Wrong input " <<   chRefecAns << endl << endl;
              cout << "Please try again." << endl << endl;
            }