Structures

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Structures

 
0
  #1
Jan 3rd, 2009
I'm working with structures, but somehow it skips one of the user inputs for the pie's price:
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <string>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. const int ESC = 0x1b;
  9.  
  10. struct pie{
  11. string brand;
  12. float price;
  13. string flavour;
  14. }pies[5];
  15.  
  16. int main()
  17. {
  18. cout << "Structures.";
  19. cout << "\nPress c to contine.";
  20. int a;
  21. while ( (a = getch()) != ESC)
  22. if (a == 'c')
  23. {
  24. int n;
  25. int g;
  26. for (n = 0; n<5; n++)
  27. {
  28. cout << "\nEnter brand of pie:";
  29. getline(cin,pies[n].brand);
  30. cout << "\nEnter price of pie:";
  31. cin >> g;
  32. pies[n].price = g;
  33. cout << "\nEnter Flavour of pie:";
  34. getline(cin,pies[n].flavour);
  35. }
  36. getch();
  37. return 0;
  38. }
  39. }


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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Structures

 
0
  #2
Jan 3rd, 2009
There's plenty of ways around the many problems associated with 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.
  1. bool read_float( std::istream& in , float& num )
  2. {
  3. std::string str;
  4. std::getline( in, str );
  5. std::istringstream iss( str );
  6. iss >> num;
  7. return iss.good();
  8. }
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.
  1. cout << "\nEnter price of pie:";
  2. while ( ! read_float( std::cin , g ) )
  3. 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)
  1. template <typename Ty>
  2. bool read_in( std::istream& in , Ty& num )
  3. {
  4. std::string str;
  5. std::getline( in, str );
  6. std::istringstream iss( str );
  7. iss >> num;
  8. return iss.good();
  9. }
Last edited by Bench; Jan 3rd, 2009 at 8:31 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC