Menu

do while loops

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

    Is there anyway that I can set this loop up so that if a word other than dec, jan, or feb is inputed that it just shows the 1.47 value?? thanks
    ouble total=0;       //Initalizes the variables.
    double price=0;
    double Price_Grapes=1.47;
    double amount=0;
    double subtotal=0;
    int grapes=0;
    char szKey[]="dec,jan,feb";
    char szInput[80];
    char first_three[]="mar,apr,may,jun,jul,aug,sept,oct,nov";
    char first_threeInput[80];
    do{
    printf ("Enter first three letters of the month\n");
    gets (szInput);
    }while (strcmp(szKey,szInput)==0);

        //set keyboard input
    printf ("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.
        cout.setf(ios::showpoint |ios::fixed);   //sets return values.
        cout<<setprecision(2);
        subtotal=2.00*amount;
        cout<<"$"<<subtotal<<'\n';   //informs user of subtotal.ifin
    do{
    printf ("Enter first three letters of the month\n");
    gets (first_threeInput);
    }while (strcmp(first_three,first_threeInput)==0);

        //set keyboard input
    printf ("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.
        cout.setf(ios::showpoint |ios::fixed);   //sets return values.
        cout<<setprecision(2);
        subtotal=1.47*amount;
        cout<<"$"<<subtotal<<'\n';

     
    • Curtis Sutter

      Curtis Sutter - 2002-12-11

      char *szKey[] = {"dec","jan","feb","\n"};
      bool firstthree = false;

      printf ("Enter first three letters of the month\n");
      gets (first_threeInput);
      for (count = 0; szKey[count] != "\n"; count++)
      {
        if(strcmp(szKey[count], first_threeInput) == 0)
        firstthree = true;
      }

      subtotal = (firstthree?1.47*amount:1.47);

      /* The key is set as a dbl array of char so you can have many strings.  You need the "\n" for the for loop.  After entering a value, the for loop will check it against the key values, if found it changes the value of firstthree to true.  Using the bool firstthree, I can then set the subtotal to what I want.
      As for the way you were doing it, there is a function in C that will parse a string for a "sub-string".  It will return a pointer to where it is located.  Kip has used this before and is better equipped to tell you about it, since I only really know C++.  This function would do-away with my for loop, making the code more efficient.

      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.