943,746 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 326
  • C++ RSS
Jan 3rd, 2009
0

Structures

Expand Post »
I'm working with structures, but somehow it skips one of the user inputs for the pie's price:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Jan 3rd, 2009
0

Re: Structures

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.
cpp Syntax (Toggle Plain Text)
  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.
cpp Syntax (Toggle Plain Text)
  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)
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Quick question
Next Thread in C++ Forum Timeline: what is the name of all c++ exception handlers?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC