473,623 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ having probs with getline function

5 New Member
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 2856
Savage
1,764 Recognized Expert Top Contributor
Hi,can u post just troubleshooting part because this code is just to big?

Savage
Apr 22 '07 #2
sicarie
4,677 Recognized Expert Moderator Specialist
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 New Member
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(sea ts[row][coloumn].name); // trying to use getline() for input to text file
Apr 23 '07 #4
ilikepython
844 Recognized Expert Contributor
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(sea ts[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 New Member
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 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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

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

Similar topics

5
7746
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program that shows how to use the 'getline' function. It said: "The getline function allows you to specify how many bytes you will get from the users input. Each character the user's types takes up one byte. So if, in the getline function, you specify...
1
2141
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I could state getline's first parameter supports "FROM". The second parameter supports "TO". // later
10
5605
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers I have a data file called Standard.udf The Data File (not the problem): The data in the file is in text format, and was created using a function in this program with...
14
3873
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy enough, but I can't get the getline(file_name array_name) to work. So...I am thinking it is definitely...
18
8244
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include <iostream> #include <fstream> #include <cstdlib> #include <string>
4
2155
by: interpim | last post by:
ok the function is supposed to accept only non-negative integers... but it still accepts letters input through the function. Such as '23e' still passes through. I tried to use isalpha in the or statement to cause an error, but it causes an error for everything input. How do I make an error occur when anything other than numbers are put in? #include <stdio.h> #define MAXLINE 20 int main(void) { char line; int error,n; do{...
9
1747
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using namespace std; const MAX_CLASSES = 12;
33
25200
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
2
1427
by: FightingWolf | last post by:
Hey, first of all sorry for my bad english, but I'm from Germany ;) I found something special in the getline() function, I don't understand. If I use the getline function in case of an Excel-sheet or an image - to get the image-data - the getline function breakes after a NULL termination ( \0 ) fstream oFile_e;
0
8165
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8670
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8613
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4074
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1778
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.