471,066 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

C++ having probs with getline function

5
I want to use the getline() so that i can enter a entire name in on line. (with spaces)
The prob is that i am initializing the variable as "N/A" and saving it to a text file. it is declared as a string. look at the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string>
  5. using namespace std;
  6.  
  7. //Account data structure
  8. struct account
  9. {
  10.  string user;
  11.  string enter_password;
  12. };
  13.  
  14. //Dining Data Structure
  15. struct Dining
  16. {
  17.  account username;
  18.  string name; // the variable that i'm using to store the name 
  19.  string time;
  20.  int persons;
  21.  char status;
  22. };
  23.  
  24. // Array declaration
  25. Dining seats[8][4];
  26. char reserve[] = "C:\\Seat Reserving.txt";
  27.  
  28. void show();
  29.  
  30. void username_entry();
  31.  
  32. /*
  33. password user_pass[5];
  34. char username[] = "C:\\Username Password Storage";
  35. */
  36.  
  37. void initialise();
  38.  
  39. void clear();
  40.  
  41. //Seats reserving
  42. void to_reserve();
  43. int input();
  44.  
  45. void showed_up();
  46.  
  47.  
  48. int main()
  49. {
  50.   ifstream check_b4_start;
  51.  
  52.   check_b4_start.open(reserve);
  53.  
  54.    if(check_b4_start.fail())
  55.     {
  56.       cout<<"The file failed to open"<<endl;
  57.       cout<<"No file was found"<<endl;
  58.       cout<<"File created"<<endl;
  59.       initialise();
  60.       system("PAUSE");
  61.       system ("cls");
  62.     }
  63.    to_reserve();
  64.    //show();
  65.   //username_entry();
  66.  
  67.  
  68.       system("PAUSE");
  69.       return 0;
  70. }
  71.  
  72. void initialise()
  73.  {
  74.   ofstream ini; //varible declared for input
  75.  
  76.   ini.open(reserve);
  77.  
  78.   if (ini.fail())
  79.    {
  80.     cout<<"The file failed to open"<<endl;
  81.     system ("PAUSE");
  82.     exit(1);
  83.    }
  84.  
  85.   else
  86.    {
  87.    // cout<<"The file opened sucessfully"<<endl;
  88.  
  89.       for (int n = 0; n < 8; n++)
  90.        {
  91.          for (int x = 0; x < 4; x++)
  92.           {
  93.             seats[n][x].username.user = "N/A";
  94.             seats[n][x].username.enter_password = "N/A";
  95.             seats[n][x].name = "N/A";  // initialization of the variable to text file
  96.             seats[n][x].time = "N/A";
  97.             seats[n][x].persons = 0;
  98.             seats[n][x].status = 'E';
  99.  
  100.             ini<<seats[n][x].username.user<<endl;
  101.             ini<<seats[n][x].username.enter_password<<endl;
  102.             ini<<seats[n][x].name<<endl;
  103.             ini<<seats[n][x].time<<endl;
  104.             ini<<seats[n][x].persons<<endl;
  105.             ini<<seats[n][x].status<<endl;
  106.             ini<<endl;
  107.          }
  108.        }
  109.  
  110.       ini.close();
  111.  
  112.         if (ini.fail())
  113.          {
  114.           cout<<"The file failed to close"<<endl;
  115.           system ("PAUSE");
  116.           exit(1);
  117.          }
  118.    }
  119. }
  120.  
  121.  
  122.  
  123. void show()
  124.  {
  125.   ifstream showing;
  126.  
  127.   showing.open(reserve);
  128.  
  129.   if (showing.fail())
  130.    {
  131.     cout<<"The file failed to open"<<endl;
  132.     system ("PAUSE");
  133.     exit(1);
  134.    }
  135.  
  136.   else
  137.    {
  138.     cout<<"The file open sucessfully"<<endl;
  139.     cout<<"\nBelow shows which seats are free"<<endl;
  140.     cout<<"\n  0 1 0 1 2 3 2 3"<<endl;
  141.  
  142.     for (int n = 0; n < 8; n++)
  143.      {
  144.       for ( int x = 0; x < 4; x++)
  145.        {
  146.         showing>>seats[n][x].username.user;
  147.         showing>>seats[n][x].username.enter_password;
  148.         showing>>seats[n][x].name;
  149.         showing>>seats[n][x].time;
  150.         showing>>seats[n][x].persons;
  151.         showing>>seats[n][x].status;
  152.  
  153.         cout<<seats[n][x].status;
  154.  
  155.             if (x == 0)
  156.              {
  157.               cout<<n;
  158.              }
  159.  
  160.  
  161.             if ((n < 2) && (x == 0))
  162.              {
  163.               cout<<"    ";
  164.              }
  165.  
  166.             if ((n > 1) && (n < 6) && (x == 2))
  167.              {
  168.               cout<<"        ";
  169.              }
  170.  
  171.  
  172.             if ((n > 5) && (x == 0))
  173.              {
  174.               cout<<"    ";
  175.              }
  176.  
  177.        cout<<seats[n][x].status;
  178.        }
  179.       cout<<endl;
  180.       }
  181.    }
  182. }
  183.  
  184. void username_entry()
  185.  {
  186.   char user_entered[10];
  187.   string password;
  188.   ifstream entry;
  189.   bool enter_correct = false;
  190.  
  191.   bool name_bad = false;
  192.   bool pass_bad = false;
  193.  
  194.   while (enter_correct == false)
  195.    {
  196.      cout<<"Please enter username- ";
  197.      cin.getline(user_entered,10); 
  198.      cout<<endl;
  199.  
  200.      cout<<"Enter password- ";
  201.      cin>>password;
  202.      cout<<endl;
  203.  
  204.      entry.open(reserve);
  205.  
  206.      if(entry.fail())
  207.       {
  208.        cout<<"The file failed to open"<<endl;
  209.        system ("PAUSE");
  210.        exit(1);
  211.       }
  212.  
  213.      else
  214.       {
  215.         for (int n = 0; n < 8; n++)
  216.          {
  217.            for ( int x = 0; x < 4; x++)
  218.             {
  219.              entry>>seats[n][x].username.user;
  220.              entry>>seats[n][x].username.enter_password;
  221.              entry>>seats[n][x].name;
  222.              entry>>seats[n][x].time;
  223.              entry>>seats[n][x].persons;
  224.              entry>>seats[n][x].status;
  225.  
  226.              if ((seats[n][x].username.user == user_entered) && (seats[n][x].username.enter_password == password))
  227.               {
  228.                enter_correct = true;
  229.               }
  230.  
  231.              if ((seats[n][x].username.user == user_entered) && (seats[n][x].username.enter_password != password))
  232.               {
  233.                pass_bad = true;
  234.               }
  235.  
  236.              if ((seats[n][x].username.user != user_entered) && (seats[n][x].username.enter_password != password))
  237.               {
  238.                pass_bad = true;
  239.                name_bad = true;
  240.               }
  241.              if ((seats[n][x].username.user != user_entered) && (seats[n][x].username.enter_password == password))
  242.               {
  243.                name_bad = true;
  244.               }
  245.             }
  246.          }
  247.  
  248.          if ((name_bad == true) && (pass_bad == false))
  249.           {
  250.            cout<<"There is no such person found"<<endl;
  251.           }
  252.  
  253.          if ((pass_bad == true) && (pass_bad == false))
  254.           {
  255.            cout<<"You entered the password incorrectly"<<endl;
  256.           }
  257.  
  258.          if ((pass_bad == true) && (name_bad == true))
  259.           {
  260.            cout<<"That person and password is not found"<<endl;
  261.           }
  262.  
  263.          if (enter_correct == true)
  264.           {
  265.            cout<<"You have entered the correct password or username"<<endl;
  266.           }
  267.  
  268.       entry.close();
  269.  
  270.           if (entry.fail())
  271.            {
  272.             cout<<"The file failed to close"<<endl;
  273.             system ("PAUSE");
  274.             exit(1);
  275.            }
  276.       }
  277.    }
  278.   system ("cls");
  279.   cout<<"\n                        WELCOME"<<endl;
  280.  }
  281.  
  282. void to_reserve()
  283.  {
  284.   show();
  285.  
  286.   int mini_ch;
  287.   bool exit = false;
  288.   int ex_ch = input();
  289.  
  290.   cout<<"1- To reserve seats"<<endl;
  291.   cout<<"2- exit to menu"<<endl;
  292.   cin>>mini_ch;
  293.  
  294.  while (exit == false)
  295.   {
  296.     if(mini_ch == 1)
  297.      {
  298.       input();
  299.       show();
  300.      }
  301.  
  302.     if (mini_ch == 2)
  303.      {
  304.       exit = true;
  305.      }
  306.  
  307.     if (ex_ch == 0)
  308.       {
  309.        exit = true;
  310.       }
  311.  
  312.     else
  313.      {
  314.        cout<<"Invalid Entry"<<endl;
  315.      }
  316.    }
  317. }
  318.  
  319. int input()
  320. {
  321.   ofstream inp;
  322.   int row, coloumn;
  323.   char decision;
  324.   int exit_ch = 2;
  325.  
  326. while (exit_ch == 2)
  327. {
  328.  cout<<"Please enter the row and column for the seat you want- "<<endl;
  329.  cin>>row;
  330.  cout<<endl;
  331.  cin>>coloumn;
  332.  
  333.  cout<<"\nWho is reserving this seat";
  334.  cin.getline(seats[row][coloumn].name); //trying to use getline() to read the name in
  335.  
  336.  cout<<"\nWhat time would you like";
  337.  cin>>seats[row][coloumn].time;
  338.  
  339.  cout<<"\nHowmany persons would be joining you- ";
  340.  cin>>seats[row][coloumn].persons;
  341.  cout<<endl;
  342.  seats[row][coloumn].status = 'R';
  343.  
  344.  cout<<"Do you want to save this configuration (y/n)- ";
  345.  cin>>decision;
  346.  cout<<endl;
  347.  
  348.     inp.open(reserve);
  349.  
  350.     if (inp.fail())
  351.      {
  352.       cout<<"The file failed to open"<<endl;
  353.       system ("PAUSE");
  354.       exit(1);
  355.      }
  356.  
  357.     else
  358.      {
  359.         if (decision == 'y')
  360.          {
  361.           //inp<<seats[row][coloumn].name;
  362.           inp<<seats[row][coloumn].time;
  363.           inp<<seats[row][coloumn].persons;
  364.           inp<<seats[row][coloumn].status;
  365.           exit_ch = 0;
  366.          }
  367.  
  368.          if (decision == 'n')
  369.           {
  370.            cout<<"1- to re-enter the data"<<endl;
  371.            cout<<"2- to exit to menu"<<endl;
  372.            cin>>exit_ch;
  373.           }
  374.        inp.close();
  375.  
  376.          if (inp.fail())
  377.           {
  378.            cout<<"The file failed to close"<<endl;
  379.            system ("PAUSE");
  380.            exit(1);
  381.           }
  382.      }
  383.  }
  384. return exit_ch;
  385. }
sorry for the lack of documentation.
Apr 22 '07 #1
8 2614
Savage
1,764 Expert 1GB
Hi,can u post just troubleshooting part because this code is just to big?

Savage
Apr 22 '07 #2
sicarie
4,677 Expert Mod 4TB
sabby-

Please take a look at our Posting Guidelines, specifically the part about not posting full code, but rather the section that you're having trouble with, as well as the part about using code tags.
Apr 22 '07 #3
sabby
5
sorry guys. i want to know how to make that little window with the scroll bar to help minimize the space it takes up.

//Account data structure
struct account
{
string user;
string enter_password;
};

//Dining Data Structure
struct Dining
{
account username;
char* name;
string time;
int persons;
char status;
};

seats[n][x].name = "N/A";//this clashes with the below input
//it is saved as a text file

cout<<"\nWho is reserving this seat";
cin.getline(seats[row][coloumn].name); // trying to use getline() for input to text file
Apr 23 '07 #4
ilikepython
844 Expert 512MB
sorry guys. i want to know how to make that little window with the scroll bar to help minimize the space it takes up.

//Account data structure
struct account
{
string user;
string enter_password;
};

//Dining Data Structure
struct Dining
{
account username;
char* name;
string time;
int persons;
char status;
};

seats[n][x].name = "N/A";//this clashes with the below input
//it is saved as a text file

cout<<"\nWho is reserving this seat";
cin.getline(seats[row][coloumn].name); // trying to use getline() for input to text file
You use code tags ([code'] code here [/code'] but w/o the apostrophe).
Also, getline takes two arguements. You have only one. You need another telling you the size of the data you want to get from the stream. That should probably fix your problem.
Apr 23 '07 #5
sabby
5
You use code tags ([code'] code here [/code'] but w/o the apostrophe).
Also, getline takes two arguements. You have only one. You need another telling you the size of the data you want to get from the stream. That should probably fix your problem.
So, even if i have the variable declared as a string i have to put an other argument to show how many characters?
Apr 23 '07 #6
ilikepython
844 Expert 512MB
So, even if i have the variable declared as a string i have to put an other argument to show how many characters?
Yes,
so for example if I do this:
Expand|Select|Wrap|Line Numbers
  1. string mystring;
  2. cin.getline(mystring, 10);
  3.  
and the user enters "Some random string", mystring will get "Some rando".
Does that help?
Apr 23 '07 #7
sabby
5
Yes,
so for example if I do this:
Expand|Select|Wrap|Line Numbers
  1. string mystring;
  2. cin.getline(mystring, 10);
  3.  
and the user enters "Some random string", mystring will get "Some rando".
Does that help?
THANK YOU SOOO MUCH. that was stressing me out and all i had to do was add a limiter.
Apr 23 '07 #8
ilikepython
844 Expert 512MB
THANK YOU SOOO MUCH. that was stressing me out and all i had to do was add a limiter.
You are very welcome.
Apr 23 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by vknid | last post: by
1 post views Thread by ma740988 | last post: by
10 posts views Thread by Skywise | last post: by
14 posts views Thread by KL | last post: by
18 posts views Thread by Amadeus W. M. | last post: by
9 posts views Thread by Jordan Tiona | last post: by
33 posts views Thread by Chen shuSheng | last post: by
2 posts views Thread by FightingWolf | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.