[c++] wont stay open

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

i have this very simple c++ start of a progam i am making but when i run it and type in the first thing and hit enter it "termenates" on me my just closing why?


Code: [ Select ]
#include <iostream.h>

int main()
{
char pageName, varifi;

 //input the file name
 cout << "Enter the page name: " << endl;
 cin >> pageName;
 
 //varification
 cout << "The page name up enter was " << pageName << endl;
 cout << "Is this correct? yes no " << endl;
 cin >> varifi;
 

 
}
  1. #include <iostream.h>
  2. int main()
  3. {
  4. char pageName, varifi;
  5.  //input the file name
  6.  cout << "Enter the page name: " << endl;
  7.  cin >> pageName;
  8.  
  9.  //varification
  10.  cout << "The page name up enter was " << pageName << endl;
  11.  cout << "Is this correct? yes no " << endl;
  12.  cin >> varifi;
  13.  
  14.  
  15. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

The problem is that you are prompting the user to enter a string of characters but you've declared pageName as a single char so it will only hold a single character and cin is only going to read the first character into it. The rest of what the user types will still be sitting in the input buffer waiting to be read. The next time cin is called it's not going to put a cursor to the screen to prompt for input. It will just read the next character from the buffer, and since there is nothing else to be done the program will just end.

Say I enter "index" for the pageName: the "i" will be read into pageName and then the next cout prompts will be put to the screen and the next cin will read the "n" of index into varifi and the program will terminate. The process happens so fast that you won't even see the second prompts on the screen before the window closes. One quick way to test if this is happening is run it again and enter a single character for the pageName and then see what happens.

One solution is to declare pageName as an array of chars with a maximum permissable size and then use cin.getline to read the entire string of characters into the pageName variable:

Code: [ Select ]
const int MAXSIZE = 100;
char pageName[MAXSIZE], varifi;
cout << "Enter the page name: " << endl;
cin.getline(pageName,MAXSIZE);
  1. const int MAXSIZE = 100;
  2. char pageName[MAXSIZE], varifi;
  3. cout << "Enter the page name: " << endl;
  4. cin.getline(pageName,MAXSIZE);


There is also a string object in C++ but I haven't used it since school, and I'm on vacation without any of my books handy. Anyway, if you're just starting out with C++ you usually start with strings as array of characters work your way up to using objects (at least we did in my C++ class).
  • hari
  • Born
  • Born
  • hari
  • Posts: 2
  • Loc: India

Post 3+ Months Ago

For taking multiple characters as input, first declare the variable that holds the set of characters as a character array and then use the member function getline belonging to class cin. This will take the whole line as input and store it in the name variable.
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

better still, use the c++ standard library class string
(which grows automatically to fit what's put into it):

Code: [ Select ]
#include <string>
#include <iostream>
using namespace std;

int main()
{
  //input the file name
  string pageName;
  cout << "Enter the page name: " << endl;
  cin >> pageName;

  //verification
  string verify;
  cout << "The page name up enter was " << pageName << endl;
  cout << "Is this correct? yes no " << endl;
  cin >> verify;

  // comparision is easy with strings
  if( verify == "yes" )
  {
     // .....
  }

   // ....
}
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.   //input the file name
  7.   string pageName;
  8.   cout << "Enter the page name: " << endl;
  9.   cin >> pageName;
  10.   //verification
  11.   string verify;
  12.   cout << "The page name up enter was " << pageName << endl;
  13.   cout << "Is this correct? yes no " << endl;
  14.   cin >> verify;
  15.   // comparision is easy with strings
  16.   if( verify == "yes" )
  17.   {
  18.      // .....
  19.   }
  20.    // ....
  21. }

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 112 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.