| | |
Structures
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I'm working with structures, but somehow it skips one of the user inputs for the pie's price:
When I get up to Enter brand of pie, then I press enter, it skips straight onto Enter Flavour of pie leaving the price of pie blank. I've tried cin.ignore() but that doesn't seem to do it. Any help would be appreciated.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <windows.h> #include <string> #include <conio.h> using namespace std; const int ESC = 0x1b; struct pie{ string brand; float price; string flavour; }pies[5]; int main() { cout << "Structures."; cout << "\nPress c to contine."; int a; while ( (a = getch()) != ESC) if (a == 'c') { int n; int g; for (n = 0; n<5; n++) { cout << "\nEnter brand of pie:"; getline(cin,pies[n].brand); cout << "\nEnter price of pie:"; cin >> g; pies[n].price = g; cout << "\nEnter Flavour of pie:"; getline(cin,pies[n].flavour); } getch(); return 0; } }
When I get up to Enter brand of pie, then I press enter, it skips straight onto Enter Flavour of pie leaving the price of pie blank. I've tried cin.ignore() but that doesn't seem to do it. Any help would be appreciated.
There's plenty of ways around the many problems associated with
My personal preference is to keep a separate function which grabs a line using getline(), and a stringstream to convert your input to whatever numeric type you need. This has the added bonus of removing the risk that cin will fail due to invalid chars being typed. You can check for this after asking the user to enter the price of the pie.
One slight nitpick - the default floating-point type in C++ is double. For that reason its usually recommended to always use double whenever you need floating point numbers, unless you have some compelling reason otherwise to use float (which generally suffers from low precision. usually only 5 or 6 s.f. on most modern machines)
edit if you want to be extra-clever you can template the 'read' function to work with other numeric types (which would be handy if you wanted to do the same with int or double)
cin >>; IMHO the best way to avoid them is to never use the >> operator in conjunction with cin in the first place (unless its really necessary).My personal preference is to keep a separate function which grabs a line using getline(), and a stringstream to convert your input to whatever numeric type you need.
cpp Syntax (Toggle Plain Text)
bool read_float( std::istream& in , float& num ) { std::string str; std::getline( in, str ); std::istringstream iss( str ); iss >> num; return iss.good(); }
cpp Syntax (Toggle Plain Text)
cout << "\nEnter price of pie:"; while ( ! read_float( std::cin , g ) ) std::cout << "Invalid input - Enter price of pie: ";
One slight nitpick - the default floating-point type in C++ is double. For that reason its usually recommended to always use double whenever you need floating point numbers, unless you have some compelling reason otherwise to use float (which generally suffers from low precision. usually only 5 or 6 s.f. on most modern machines)
edit if you want to be extra-clever you can template the 'read' function to work with other numeric types (which would be handy if you wanted to do the same with int or double)
cpp Syntax (Toggle Plain Text)
template <typename Ty> bool read_in( std::istream& in , Ty& num ) { std::string str; std::getline( in, str ); std::istringstream iss( str ); iss >> num; return iss.good(); }
Last edited by Bench; Jan 3rd, 2009 at 8:31 am.
¿umop apisdn upside down? ![]() |
Similar Threads
- Creating dynamic array structures (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- dynamic array of structures problem (C++)
- C++ structures (C++)
- Read and writing strings from structures (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the C++ Forum
- Previous Thread: Help with manually creating a BMP image
- Next Thread: what is the name of all c++ exception handlers?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





