HELP with input char

Hi,

I am trying to input a character and right away an array of characters...
However once i input the character, it doesnt let me input the array character and the program ends...

Consider this:

1
2
3
4
5
6
7
  char grade[4] ={NULL};
  cout << "Enter grade: ";
  grade[0] = cin.get();  // I am inputting a character here...
    
  char name[100] = " ";
  cout << endl << "Enter name: ";  
  cin.getline(name, 80, '\n');  // ..but i cannot input here a string..The program avoids it and it ends. 



Any advice here?

Thank you
Hi,
I suggest using std:string as it simplifies and makes it more robust.

1
2
3
4
5
6
7
8
9
        std::string  grade, name;

        std::cout << "Enter grade: ";
        std::cin >> grade;

        std::cout << "Enter name: ";
        std::cin >> name;

        std::cout << grade << ", " << name << std::endl;


but, you need to control yourself the input validity (grade length <=3 and name length <=99)

if you want to use char arrays, you need to be careful with buffer length.
Cheers
whats the std:: for?
is it because u havent written using namespace std?
oops, sorry for this.
Yes, you're true.

That's namespace stuff.
I don't like writing "using namespace"
Because, you take the habit to always use it even in header files (.h or .hpp file).
If you do, it means anyone including your header file is also having all the using namespace lines. And this could be dangerous, if the user also use namespaces.

Let's say you use your own namespace called IPV (InputValidation) and another user including your header files also use a namespace called IPV.
then, this user may have clashes with classes names. If you have a class named let's say Validator defined in your IPV namespace and the user is also define the same class Validator in his namespace IPV, this is going to make problems.

your files:
Validator.hpp
1
2
3
4
5
6
7
8
...
namespace IPV {
...
    class Validator {
    ...
    };
...
}


Interface.hpp
1
2
3
4
#include <Validator.hpp>
...
blabla
...


the user file:
Validation.hpp
1
2
3
4
5
6
7
8
9
#include <Interface.hpp>

namespace IPV {
...
   class Validator {
   ...
   };
...
}


But this occurs in big c++ projects.
So, I took the habit no to use "using namespace"
as u see im a beginner and i dont really know wht ur talking about XD
Last edited on
@Greek:
1
2
3
4
5
6
7
  char grade[4] ={NULL};
  cout << "Enter grade: ";
  grade[0] = cin.get();  // I am inputting a character here...
    
  char name[100] = " ";
  cout << endl << "Enter name: ";  
  cin.getline(name, 80, '\n');  // ..but i cannot input here a string..The program avoids it and it ends.  


The problem you are facing comes from the STDIN buffer. What is happening is your std::cin.get() Reads this as input (for example):

A<Enter>
So it stores A into grade[0]. All good and dandy. But the problem here i that you entered two characters! Wheres the second one? Its the enter character otherwise know as '\n'! The enter key counts as a character in the STDIN buffer for std::cin. Now it gets to the next std::cin and it gets told read up to 80 characters in the STDIN buffer or up to the first '\n' character. Seeing as we didn't flush this buffer before we called this guy we basically give him all he needed to finish his command and he exits. Your character array fills with just a single newline character, a null terminator and that's it.

The fix for all this is simple. Before calling std::cin.getline() call std::cin.ignore().
That will ignore the newline character in the buffer.

So you should just have this:
1
2
3
4
5
6
7
8
  char grade[4] ={NULL};
  cout << "Enter grade: ";
  grade[0] = cin.get();  // I am inputting a character here...
    
  char name[100] = " ";
  cout << endl << "Enter name: ";  
  cin.ignore();
  cin.getline(name, 80, '\n');  // ..but i cannot input here a string..The program avoids it and it ends.  


Try it and see what happens.
@wolfgang

Thanks a lot man! I tried 4 forums and no one could give me a correct and definite answer!
Topic archived. No new replies allowed.