?
Solved

problem on second getline loop

Posted on 2005-03-30
4
Medium Priority
?
265 Views
Last Modified: 2008-02-01
I have created a project that requests the user to enter and rate pizza, however my current code doesnt seem to work right after the first interation. when it asks the user if they want to rate another pizza and they say yes it skips past the first question that requests for input of the pizza name

Here is my code.  (whats wrong... help oh coding gods)



#include <iostream.h>
#include <stdlib.h>
#include <cmath>
#include <string>
#include <fstream>

int main()
{
      char name[256], name2[256], name3[256];
      float dia[3];
      float oz[3];
      int score[3];
      char fileName[30] = "MyPizza";
      string sp = " ";
      string d = " - ";
      char re;



         cout << "\t\t*******************************\n";
         cout << "\t\t****** Pizza Analyzers ********\n";
         cout << "\t\t*******************************\n\n";


         cout << "Please enter the name of one pizza:  ";
         cin.getline (name,256);


         cout << "\n\nPlease enter the diameter of the pizza in inches :  ";
         cin >> dia[0];

         cout << "\n\nPlease enter the weight in ounces :  ";
         cin >> oz[0], a >> dummychar >> b;

         cout << "\n\nPlease enter your rating for this pizza, (0-5) :  ";
         cin >> score[0];

         cout << "Would you like to rate another pizza? (y/n) :  ";
         cin >> re;

         if (re == 'y') {
            system("cls");

            cout << "\n\nPlease enter the name of one pizza:  ";
            cin.getline (name2,256);

            cout << "\n\nPlease enter the diameter of the pizza in inches :  ";
            cin >> dia[1];

            cout << "\n\nPlease enter the weight in ounces :  ";
            cin >> oz[1];

            cout << "\n\nPlease enter your rating for this pizza, (0-5) :  ";
            cin >> score[1];

            if (score[1] >= 6) {
               cout << "Please enter a rating from 0-5 :  ";
               cin >> score[1];
            }

            cout << "Would you like to rate another pizza? (y/n) :  ";
            cin >> re;

            if (re == 'y') {
               system("cls");

               cout << "Please enter the name of one pizza excluding all titles :  ";
               cin.getline (name3,256);

               cout << "\n\nPlease enter the diameter of the pizza in inches :  ";
               cin >> dia[2];

               cout << "\n\nPlease enter the weight in ounces :  ";
               cin >> oz[2];

               cout << "\n\nPlease enter your rating for this pizza, (0-5) :  ";
               cin >> score[2];

               if (score[2] >= 6) {
                  cout << "Please enter a rating from 0-5 :  ";
                  cin >> score[2];
               }

               system("cls");

               cout << name[0] << sp << d << dia[0] << " diameters " << oz[0] << " ounces with a rating of : " << score[0] << endl << endl;
               cout << name[1] << sp << d << dia[1] << " diameters " << oz[1] << " ounces with a rating of : " << score[1] << endl << endl;
               cout << name[2] << sp << d << dia[2] << " diameters " << oz[2] << " ounces with a rating of : " << score[2] << endl << endl;

               ofstream Pizza(fileName, ios::out);
               Pizza << name[0] << sp << d << dia[0] << " diameters " << oz[0] << " ounces with a rating of : " << score[0] << endl;
               Pizza << name[1] << sp << d << dia[1] << " diameters " << oz[1] << " ounces with a rating of : " << score[1] << endl;
               Pizza << name[2] << sp << d << dia[2] << " diameters " << oz[2] << " ounces with a rating of : " << score[2] << endl;
            }
            else {
                 system("cls");

                 cout << name[0] << sp << d << dia[0] << " diameters " << oz[0] << " ounces with a rating of : " << score[0] << endl << endl;
                 cout << name[1] << sp << d << dia[1] << " diameters " << oz[1] << " ounces with a rating of : " << score[1] << endl << endl;

                 ofstream Pizza(fileName, ios::out);
                 Pizza << name[0] << sp << d << dia[0] << " diameters " << oz[0] << " ounces with a rating of : " << score[0] << endl;
                 Pizza << name[1] << sp << d << dia[1] << " diameters " << oz[1] << " ounces with a rating of : " << score[1] << endl;
            }
         }
         else {
              system("cls");

              cout << name[0] << sp << d << dia[0] << " diameters " << oz[0] << " ounces with a rating of : " << score[0] << endl;

              ofstream Pizza(fileName, ios::out);
              Pizza << name[0] << sp << d << dia[0] << " diameters " << oz[0] << " ounces with a rating of : " << score[0] << endl;
         }

      system("PAUSE");
      return 0;
}
0
Comment
Question by:xardious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:xardious
ID: 13669434
lastly how can i get this program to except fractions as well for input?
0
 

Author Comment

by:xardious
ID: 13669446
please ignore / delete this part on lline 25 , a >> dummychar >> b
0
 
LVL 9

Expert Comment

by:rcarlan
ID: 13669513
Change the declaration of 're' from:

char re;

to:

char re[256];


You will also have to change:

if (re == 'y')

to

if (_stricmp(re, "y") == 0)


Depending on the compiler you're using, you may have to use _strcmpi instead of _stricmp. Also, you may have to drop the leading underscore.


Finally, you may want to consider using a loop instead of three separate input blocks. Something like:

do
{
    system("cls");

    cout << "\n\nPlease enter the name of one pizza:  ";

    // ...

    cout << "Would you like to rate another pizza? (y/n) :  ";
    cin >> re;

} while (_stricmp(re, "y") == 0);


That is, assuming you've already covered do-while loops in class ;-)

Accepting fractions as input (i.e. 2/3) is not that easy. You'd have to read them as text, then look for the '/' character, and if present convert the text preceding it to a number and the text following the '/' to another number, and divide them. You'd also have to handle the situation where the user does not enter a fraction (i.e. if you do not find '/' convert the entire input text to a number).

Radu
0
 
LVL 8

Accepted Solution

by:
novitiate earned 1500 total points
ID: 13669542
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <string>
#include <fstream>

#define MAX_RATINGS 3
using namespace std;




int main()
{
      char names[MAX_RATINGS][256];// name2[256], name3[256];
      float dia[MAX_RATINGS];
      float oz[MAX_RATINGS];
      int score[MAX_RATINGS];
      char fileName[30] = "MyPizza";
      string sp = " ";
      string d = " - ";
      char re;
      int count = 0;
      
      do{
            system("cls");
            
            cout << "\t\t*******************************\n";
            cout << "\t\t****** Pizza Analyzers ********\n";
            cout << "\t\t*******************************\n\n";
            
            
            cout << "Please enter the name of one pizza:  ";
            cin.getline (names[count],256);
            
            
            cout << "\n\nPlease enter the diameter of the pizza in inches :  ";
            cin >> dia[count];
            
            cout << "\n\nPlease enter the weight in ounces :  ";
            cin >> oz[count];// a >> dummychar >> b;
            
            cout << "\n\nPlease enter your rating for this pizza, (0-5) :  ";
            cin >> score[count];
            
            cout << "Would you like to rate another pizza? (y/n) :  ";
            cin >> re;
            cin.get();
      }while( (++count < MAX_RATINGS) && (re == 'y'));
      
      system("cls");
      ofstream Pizza(fileName, ios::out);
      
      for(int i = 0; i < count; ++i)
      {
            cout << names[i] << sp << d << dia[i] << " diameters " << oz[i] << " ounces with a rating of : " << score[0] << endl << endl;
            Pizza << names[i] << sp << d << dia[i] << " diameters " << oz[i] << " ounces with a rating of : " << score[0] << endl;
      }
      system("PAUSE");

      return 0;
}

_novi_
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question