Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

problem on second getline loop

Posted on 2005-03-30
4
Medium Priority
?
266 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
  • 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

577 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