Can I use a pointer to print only the first word of a cstring?

Hello! I want to know what I need to add to my program in order to keep it opperating properly if someone doesn't follow the directions. For example the it
says "Please enter a character > ". I only want one character but I've noticed if more then one is entered it messes up everything. How do I keep this from happening if the user enters two characters. Is there something I can add that will make the program forget all input after the first character?

I am asking this because our professor would like for us to enter good info as responses and sent her a screen shot and she also would like "bad" info entered. If it skips questions, puts extra info from the previous question into the next question, or anything else it is a fail. I need it to be able to ignore the extra info after what is asked for.

In the second question it says not to have spaces. If the users response has a space I want it to forget everything after the space. How do I do this?

In the third question it says a phrase up to 30 characters. If they put in more then 30 I want the extra to be ignored. Is the possible?? If so, how?

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Kailin Stevers
// January 30, 2017
// Challenge 1
// Character I/O in further depth

#include <iostream>
#include <string>
#include <cstring>

using namespace std;


int main()
{
	cout << endl;
	cout << " Kaitlin Stevers " << endl;
	cout << " January 30, 2017 " << endl;
	cout << " Challeng 1 " << endl;
	cout << " Character I/O in further depth. " << endl << endl;
	
	
	cout << " Using C-string character arrays " << endl;
	cout << "---------------------------------" << endl;
	cout << endl;
	const int SIZEA = 2;
	char letter[SIZEA];
	cout << " Provide a single character > " << endl;
	cin.getline(letter, SIZEA);
	cout << " The single character is: " << letter << endl;
	cout << endl;
	
	const int SIZEB = 11;
	char word[SIZEB];
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	cin.getline(word, SIZEB);
	cout << " The word is: " << word << endl;
	cout << endl;
	
	const int SIZEC =31;
	char phrase[SIZEC];
	cout << " Provide a phrase, up to 30 characters with spaces. > " << endl; 
	cin.getline(phrase, SIZEC);
	cout << " The phrase is: " << phrase << endl;
	cout << endl;
	
	
	cout << " Using sring Class Obects " << endl;
	cout << "--------------------------" << endl;
	cout << endl;
	string leter;
	cout << " Provide a single character > " << endl;
	cin >> leter;
	cin.ignore();
	cout << " The single character is: " << leter << endl;
	cout << endl;
	
	string wordd;
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	cin >> wordd;
	cin.ignore();
	cout << " The word is: " << wordd << endl;
	cout << endl;
	
	string phras;
	cout << " Provide a phrase, up to 30 characters with spaces > " << endl;
	cin.ignore();
	getline(cin, phras);
	cout << " The phrase is: " << phras << endl;
	cout << endl;
	
	return 0;
}
Last edited on
Several ways, but assuming the user inputs a string.
How do I keep this from happening if the user enters two characters.

if (leter.size() > 1)
{
// use line erase or ask them to re-enter a char.
}
What is line eraser? She wants it to continue and just take the amount it is supposed to.
Can I use a pointer to only print the first letter? How do pointers work? I'm in the middle of learning about them right now but I'm confused. Can they literally just pull the first character and display it? Or do they only work with displaying the whole string of characters stored. I'm not sure I understand them completely.

***UPDATE

I used a pointer in the first one and it works perfectly! Now onto figuring out what to do for the second question.

Can I use a pointer to pull the first word? Is that possible???
Last edited on
Question 2: read from stdin until a space is found or the array is exhausted, then ignore the rest until a newline is found.
1
2
std::cin.getline(word, SIZEB, ' '); 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
Last edited on
Thanks! I'll try that! :)

One quick extra question. I am using namespace std; (professor requires us to) how would the second line look? I assume the first is the same just drop the std. Is the second line just cin.ignore(numeric_limits < stream size > max(), 'n';
Sorry, I'm just a little confused.

I'm getting an error. Here is the line of code:
[code\]
cin.ignore(numeric_limits<streamsize>max(),'n');
[code]
Here is the error:untitled.cxx:37:39: error: expected '(' for function-style cast or type construction
Last edited on
Just missing the scope-resolution operator:
cin.ignore(numeric_limits<streamsize>::max(),'n');
You should make sure you have # include <limits> somewhere, too.
Last edited on
Eeeek! I added #include <limits> and it is still doing it :( Hummmm!

My fault!! I missed the '::' before max... what does that do??

....So new problem... When I run that the out put asks the second question but then it never prints the next line...

Here is my code verbatim:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	const int SIZEA = 1;
	char letter[SIZEA];
	cout << " Provide a single character > " << endl;
	cin >> letter;
	cin.ignore();
	cout << " The single character is: " << letter[0] << endl;
	cout << endl;
	
	const int SIZEB = 10;
	char word[SIZEB];
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	cin.getline(word, SIZEB, ' ');
	cin.ignore(numeric_limits < streamsize >:: max(), 'n');
	cout << " The word is: " << word << endl;
	cout << endl;
	
	return 0;
Last edited on
I missed the '::' before max... what does that do??

It's part of the "qualified name" -- it has to be there so the compiler knows which version of max() we're referring to.

Specifically, this whole thing is one name (and in this context the angle brackets are not greater/less-than signs):
numeric_limits<streamsize>::max()
That's not entirely true, but it's close.

Here's an example of problem 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# include <iostream>
# include <limits>

using namespace std;

int main() {
	const int SIZEB = 10;
	char word[SIZEB];
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	cin.getline(word, SIZEB, ' ');
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << " The word is: " << word << endl;
	cout << endl;
	
	return 0;
}

http://coliru.stacked-crooked.com/a/ec4635bc8be124b3

When I run that the out put asks the second question but then it never prints the next line...

I think you typo'd the newline character; you had 'n' but in this context it should be '\n'. The first one's the letter n, but the second one's a newline.

If you leave only 'n' the computer will ignore everything in standard input until an 'n' is reached. You wanted to dump out everything until the next line.
Last edited on
Thank you for explain all that! It really is appreciated! Also, you're right :) I'll try again!

**UPDATE**

That worked perfectly! Thank you so much!!!!

**UPDATE**

If the user writes under 10 characters is there something I can add to the code so that they don't have to hit enter till it reaches ten? I just noticed that if they enter something less then 10 and don't put a space at the end it is a problem. I thought the '\n' would take care of this because isn't enter a space? but I guess not in this case because it is looking at what they put after they meet 10 characters. Does that make since? If not, sorry, this complicated to ask. lol
Last edited on
WHYYYYY! GAHHH!! Can anyone tell me why it is skipping the last question??

**UPDATE** is it because I'm using can >> leter; and not cin.getline(letter);

I am trying to avoid cin.getline(letter); because I'm sure this is going to complicate things if someone puts to much info in.... gerrrrr..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <iostream>
#include <string>
#include <cstring>
#include <climits>
using namespace std;

int main()

{
	cout << endl;
	cout << " Kaitlin Stevers " << endl;
	cout << " January 30, 2017 " << endl;
	cout << " Challeng 1 " << endl;
	cout << " Character I/O in further depth. " << endl << endl;
	
	cout << " Using C-string character arrays " << endl;
	cout << "---------------------------------" << endl;
	cout << endl;
	
	const int SIZEA = 1;
	char letter[SIZEA];
	cout << " Provide a single character > " << endl;
	cin >> letter;
	cin.ignore();
	cout << " The single character is: " << letter[0] << endl;
	cout << endl;
	
	const int SIZEB = 11;
	char word[SIZEB];
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	cin.getline(word, SIZEB,' '); // The next part is a pice of code I learned today. It tells the 
	cin.ignore(numeric_limits<streamsize>::max(), '\n');//program to ignore info after the space or  	
	cout << " The word is: " << word << endl;  // outside the limits! I found it very useful here! 
	cout << endl;

	const int SIZEC =31;
	char phrase[SIZEC];
	cout << " Provide a phrase, up to 30 characters with spaces. > " << endl;
	cin.getline(phrase, SIZEC);
	cin.ignore(numeric_limits<streamsize>::max());
	cout << " The phrase is: " << phrase << endl;
	cout << endl;
	
	
	cout << " Using sring Class Obects " << endl;
	cout << "--------------------------" << endl;
	cout << endl;
	
	string leter;
	cout << " Provide a single character > " << endl;
	cin >> leter;
	cout << " The single character is: " << leter << endl;
	cout << endl;
	
	return 0;

}
Last edited on
Because line 41 ignores the maximum number of characters that can fit in a stream. This is a large number, the exact value is implementation-defined.

So remove line 41!

Next:
std::string is not a single character. It will hold a string ( https://en.wikipedia.org/wiki/String_(computer_science) ) of characters of arbitrary length.

That means that
cin.getline(letter);
is perfectly fine. That will read until the end-of-line, while std::cin >> letter will read one token.
Last edited on
Here is what I have right now. I've changed it quite a bit. lol Also removing line 41 now! :) I'll see what happens. I changed the letter part. I have to have string there. It's part of my instructors instructions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()

{
	cout << endl;
	cout << " Kaitlin Stevers " << endl;
	cout << " January 30, 2017 " << endl;
	cout << " Challeng 1 " << endl;
	cout << " Character I/O in further depth. " << endl << endl;
	
	cout << " Using C-string character arrays " << endl;
	cout << "---------------------------------" << endl;
	cout << endl;
	
	const int SIZEA = 1;
	char letter[SIZEA];
	cout << " Provide a single character > \n";
	cin >> letter;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << " The single character is: " << letter[0] << endl;
	cout << endl;
	
	const int SIZEB = 11;
	char word[SIZEB];
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	cin.getline(word, SIZEB, ' ');  
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << " The word is: " << word << endl;
	cout << endl;
	
	const int SIZEC =31;
	char phrase[SIZEC];
	cout << " Provide a phrase, up to 30 characters with spaces. > " << endl; 
	cin.getline(phrase, SIZEC, ' ');
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << " The phrase is: " << phrase << endl;
	cout << endl;	
	
	cout << " Using sring Class Obects " << endl;
	cout << "--------------------------" << endl;
	cout << endl;
	
	string leter;
	cout << " Provide a single character > " << endl;
	leter.clear();
	cin >> leter;
	cout << " The single character is: " << leter[0] << endl;
	cout << endl;
	
	string wordd;
	cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
	wordd.clear();
	cin >> wordd;
	wordd.resize(10);
	cin.ignore();
	cout << " The word is: " << wordd << endl;
	cout << endl;
	
	string phras;
	cout << " Provide a phrase, up to 30 characters with spaces > \n";
	phras.clear();
	getline(cin, phras);
	phras.resize(30);
	cin.ignore();
	cout << " The phrase is: " << phras << endl;
	cout << endl;
	
	
	
	return 0;

}


***UPDATE**8
Even without line 41 it is skipping to the bottom... :/ this is so frustrating to write. My teacher assigned it as an extra credit. God it is hard to figure out!
Last edited on
Hello katiestevers,

Good to see you back.

In answer to your new subject line I think you meant character not word, but yes you could use a pointer. But why, when "word[0]" will work just fine. If you really want to use pointer we can figure it out.

First there was nothing wrong with the program in your original post. Just your understanding of what was happening or not happening.

The first section to get one character. The code works fine as long as you type one character and press enter. But should you type two or more characters that becomes a problem. I do not claim to know the exact answer, but when you enter two or more characters the cin.getline(letter, SIZEA); on line 28 works correctly to extract a single character, but at the same time the extra character(s) cause the IO operation of cin to fail. Thus by the time you get to the next "cin.getline" cin is still in the fail state and dos not work. Add these if statements between section one and two of your original code and see what prints out when you enter more than one character compared to when you only enter one character.

1
2
3
4
5
6
	if (!std::cin)
		std::cout << "\n cin failed";
	if (std::cin.bad())
		std::cout << "\n cin bad bit set\n";
	if (std::cin.fail())
		std::cout << "\n cin fail bit set\n";


This is what I did to fix the problem. The addition is in bold:

1
2
3
4
5
6
7
8
	const int SIZEB = 11;
	char word[SIZEB]{ '\0' };  // Should always initialize variables.
	std::cout << "\n Provide a word, up to 10 characters, no spaces. > " << std::endl;
	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	std::cin.getline(word, SIZEB);
	std::cout << " The word is: " << word << std::endl;
	std::cout << std::endl;


as it has been said the std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); is also useful. Also notice that both line come before the second "cin.getline(...)" so that the getline will work properly. putting the lines
1
2
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

before the second and third std::cin.getline(...); will make the first half of the program work.

I think you may find these links useful:
http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/istream/istream/get/

Now in the second half of the program strings are much different than character arrays. You can think of a string as a variable length character array that can change size as needed. Strings are more versatile than character arrays because you can use "=" to compare two strings whereas character arrays require a function. You also have the use of + and += with strings and I think some others.

Next you need to understand how cin >> leter; works. Using this format "leter" will receive whatever you type into the keyboard be it one or 100 characters.The two things that will end input from the input buffer are a white space or enter. You may have seen something like cin >> variable1 >> variable2 and when you enter from the keyboard it looks like 123 456 with the space separating the two numbers. And what happens is that 123 goes from the input buffer to variable1, but the transfer eats the space and replaces it with "\n" leaving 456\n in the input buffer. Then the transfer to variable2 moves 456 and appends the "\n" leaving the "\n" in the input buffer. At this point any new call to cin will retrieve the "\n" from the buffer. Not rally what you want.

Another quirk of using "cin >>" and "getline(...)" is that you need to use the std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after you have used "cin >>" and before you use a "getline(...)" so the input buffer is cleared first.

If your intention is to use a pointer with the strings you could, but it is just as easy to use leter[0] or leter.at(0) and with the second version it will do bounds checking so that you do not make any mistakes.

Using wordd.resize(10); is a quick brute force way to drop any characters over ten or thirty, but it dos work. I started to work with wordd.erase(10,length - 10); which worked, but still have a couple of tests to do.

Check out these for help or more confusion:

http://www.cplusplus.com/reference/string/string/erase/
http://www.cplusplus.com/reference/string/string/resize/

Hope that helps,

Andy
Topic archived. No new replies allowed.