Menu

strings

2002-12-08
2012-09-26
  • Nobody/Anonymous

    I need to modify a working shopping program to reflect seasonal changes, can anyone help?? I need to change the price for Jan, feb and march to respond to keyboard input of first 3 letters of month, then calculate the stored amount. Any help would be appreciated.

     
    • Nobody/Anonymous

      First post all or some of the code you have the need for help with.

      j@ck_

       
    • Curtis Sutter

      Curtis Sutter - 2002-12-08

      When comparing strings (or arrays of chars), you need the strcmp() function.  Found in the string.h library.  It goes as follows...
      int strcmp(char *strone, char *strtwo);
      I will return a 0 if they are the same, a - (negitive) number if strone in < strtwo and some thing >0 if strone is < strtwo.

      Curtis

      P.S.  If you need more help, you will have to post your code.

       
    • Nobody/Anonymous

      here is my code that I am trying to work with.
      thanks for your help

      #include <iostream.h>
      #include <stdlib.h>
      #include <iomanip.h>
      #include <string.h>
      using namespace std;
           int main()    //start of program
          {
          cout<<"This is shopping by Andrew Johanesen\n"; // Creators name
          cout<<"Please press 1 for apples purchased:\n";
          cout<<"Please press 2 for grapes purchased:\n";  //creates user prompt.
          cout<<"Please Press 3 for candy bars purchased:\n";
          cout<<"Please press 4 to exit with no purchase:\n";
          const int MaxLength=4;
          char string[]="$";
          char string1[4]="dec";
          char string2[MaxLength+1];
          int month=0;
          int first_three=0;
          double total=0;
          double price=0;   //intializes the variables.
          double tax=0.06;
          double amount=0;
          double Price_Apples=.53;
          double Price_Grapes=1.47;
          double Price_Candy=1.00;
          int Apples=0;       //intializes user input for menu functions.
          int grapes=0;
          int candy=0;
          int exit=0;
          double subtotal=0;
          cin>>Apples;  //allows user to input
          if(Apples==1) //set keyboard inputs.
          cout<<"How Many Apples Purchased?\n";  //Allows user input at menu function
          cin>>amount;
          if(amount<=100&&Apples==1) //sets maxium amount that can be inputed and still allow
          //user prompts.
          cout.setf(ios::showpoint);
          cout.setf(ios::fixed);   //sets return values.
          cout<<setprecision(2);
          subtotal=.53*amount;
          cout<<"$"<<subtotal<<'\n';   //displays calculated sub-total.
          cin>>grapes;
          if(grapes==2)  //set keyboard input
          cout<<"How Many pounds of grapes Purchased?\n"; //prompts user to enter quantity.
          cin>>amount;
          if(amount<=100) //sets maxium amount that can be inputed and still produce
         //user prompts.
          int month;
          cout<<"Please enter first three letters of the month:\n";
          cin>>month;

          cout.setf(ios::showpoint |ios::fixed);   //sets return values.
          cout<<setprecision(2);
          subtotal=1.47*amount;
          cout<<"$"<<subtotal<<'\n';   //informs user of subtotal.
          cin>>candy;
          if(candy==3)  //set keyboard input.
          cout<<"How many Candy Bars?\n"; //prompts user for quantity.
          cin>>amount;
          if(amount<=100) //sets maxium amount that can be entered and produce a
          cout.setf(ios::showpoint |ios::fixed);   //sets return values.
          cout<<setprecision(2);
          subtotal=1.00*amount;
          //user prompt.
          cout<<"Your Subtotal is:\n";
          cout<<"$"<<subtotal<<'\n';   //informs user of subtotal.
          tax=subtotal*.06;
          total=tax+subtotal;
          cout<<"your total is:\n";
          cout.setf(ios::showpoint | ios::fixed);
          cout<<setprecision(2);
          cout<<"$"<<total<<'\n';
          if(exit==4)
          cout<<"Exiting program:\n";
          cin>>exit;
          system("pause");
          return 0; //end of main
          }

       
    • Nobody/Anonymous

      I was curious, and did a quick google on

      c++ "string comparison" example

      I found a good example.  I changed it to iostream, I also had to change the void main, and put in a pause.  (Brain surgery....roflmao at Wayne).  See if this example, bit the code, and the googling helps.

      Wayne

      //Using the strcmp() function. The strcmp function compares
      //strings to see if they are the same.  It returns
      //a value of zero if the two strings are the same.

      #include<iostream.h>
      #include<string.h>
      void main()
      {
          //Someone could respond in any of these ways:
          char yes1[]="yes";
          char yes2[]="Yes";
          char yes3[]="YES";

          char no1[]="no";
          char no2[]="No";
          char no3[]="NO";
         
          //This input variable will hold the user's response
          char input[4];
         
          //This is the first question
          char question_1[]="\tAre you hungry? ";
          //These are the two responses to the first question
          char response_1a[]="\tThen get some food, fool!!!\n\n\n";
          char response_1b[]="\tThat's good cause there's nothing to eat.\n\n\n";

          //2nd question and its possible responses
          char question_2[] = "\tAre you mad? ";
          char response_2a[] = "\tGet some help, dude! \n\n\n";
          char response_2b[] = "\tWhatever! \n\n\n";
         
          //Start asking questions
          //Start with question 1
          cout<<question_1;
          cin>>input;
          //The use of || in C++ means OR
          if((strcmp(input, yes1)==0) ||(strcmp(input, yes2)==0) ||(strcmp(input, yes3)==0))
          cout<<response_1a;
          else
         

          if((strcmp(input, no1)==0) ||(strcmp(input, no2)==0) ||(strcmp(input, no3)==0))
          cout<<response_1b;
         
          else
          cout<<"Incorrect response."<<endl;

          //ask question #2
          cout<<question_2;
          cin>>input;
          if((strcmp(input, yes1)==0) ||(strcmp(input, yes2)==0) ||(strcmp(input, yes3)==0))
          cout<<response_2a;
          else
         

          if((strcmp(input, no1)==0) ||(strcmp(input, no2)==0) ||(strcmp(input, no3)==0))
          cout<<response_2b;
         
          else
          cout<<"Incorrect response."<<endl;
      }

       
    • Curtis Sutter

      Curtis Sutter - 2002-12-08

      I was browsing through this code, and it needs a bit of a overhaul.  Eg.
      /* The Menu should be lowered to somewhere above this, leaving your declarations at the start of main.*/
      //Then after your menu you have...

      cin>>Apples; //allows user to input
      if(Apples==1) //set keyboard inputs.
      cout<<"How Many Apples Purchased?\n"; //Allows user input at menu function
      cin>>amount;
      if(amount<=100&&Apples==1) //sets maxium amount that can be inputed and still allow
      //user prompts.
      cout.setf(ios::showpoint);
      cout.setf(ios::fixed); //sets return values.
      cout<<setprecision(2);
      subtotal=.53*amount;
      cout<<"$"<<subtotal<<'\n'; //displays calculated sub-total.
      cin>>grapes;
      if(grapes==2) //set keyboard input
      cout<<"How Many pounds of grapes Purchased?\n"; //prompts user to enter quantity.
      cin>>amount;
      if(amount<=100) //sets maxium amount that can be inputed and still produce

      /* The error with this is that you use more than one varible with your menu, making you ask twice.  cin >> apples and cin >> grapes.  You should have a cin >> choice and if that is == 1 then run the apples, if not, then if it == 2 run grapes.  Having the extra cin's without prompts makes the program had to follow when you run it.  I suggest to following...
      - Use a cin >> choice; after the menu
      - Use a switch case on the choice.

      eg.
      // Start of switch case
      switch (choice)
      {
      case 1:
        // Apples code goes here
        break;
      case 2:
        // Grapes code goes here
        break;
      case 3:
        // Candy bars code goes here
        break;
      case 4:
        return 0;
        break;  // Not nessary like the above breaks since the program has quit.
      default:
        // Code that tell the user that the entered a bad num.
      break;  // Good coding pratice in case you add more cases.
      }
      // End of switch

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.