Fill in the blank

Apr 20, 2012 at 7:24pm
Hello, In the following program a user enters a string and then enters a number. The number will return that amount of characters in the string. It works great if the amount of characters in the string is larger than the number entered.

Example:
Input string: Michelle
Input number: 4

output: Mich

However, if the number entered is larger than the string, then the program crashes.

Example:
Input string: Michelle
Input Number: 15

output: End of days

I'm hoping to fix this so that if the number entered is larger than the string, the string is returned, followed by blank spaces for the remaining width entered.

Here is the code:

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	int ctr;
	int width;
	string a;

	cout << "Enter a string: ";getline(cin, a);
	cout << "Enter the number of letters you want to see from that string: ";
	cin >> width;

	for(ctr = 0; ctr < width; ctr++)
	{
		cout << a.at(ctr);
	}

	cout << "|";

	cin.get();
	cin.get();
	return 0;
}


Thanks ahead for your help!
Apr 20, 2012 at 7:32pm
Checking ctr against a.length() in a conditional statement will catch that undesired condition and flow as necessary.

More on usage: http://www.cplusplus.com/reference/string/string/length/
Last edited on Apr 20, 2012 at 7:36pm
Apr 20, 2012 at 7:35pm
Yes, but I would like to fill in the remaining spaces with blanks. Basically I'm trying to place multiple strings on one line that have a maximum size, but do not effect each others placement...
Apr 20, 2012 at 7:49pm
Okay. a.append() a space within a loop that iterates the difference of ctr and a.length() times when the conditional statement (as explained above) evaluates to true.

If ctr > a.length() then the difference would be the amount of spaces placed before your cout << "|" so that with your example:
Input string: Michelle
Input Number: 15

... you would end up with 7 blanks:
Michelle_______|

I think you are set. Is any of this helping? I may very well not be understanding something you want here.

Last edited on Apr 20, 2012 at 7:58pm
Apr 20, 2012 at 7:52pm
I'm new so i haven't heard of append(), but ill trying playing around with it.
Thanks for your help
Apr 20, 2012 at 8:00pm
would it go something like this?
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	int ctr;
	int width;
	int x, y;
	string a;

	cout << "Enter a string: ";getline(cin, a);
	cout << "Enter the number of letters you want to see from that string: ";
	cin >> width;

	if(width <= a.length())
	{
		for(ctr = 0; ctr < width; ctr++)
		{
			cout << a.at(ctr);
		}
	}
	else
	{
		x = a.length();
		for(x; x < width; x++)
		{
			a.append(" ");
	}

	cin.get();
	cin.get();
	return 0;
}
Apr 20, 2012 at 8:04pm
Pseudo code:
1
2
3
4
difference = ctr - a.length()
loop until  i == difference {
     a.append(" ")
} i++;


...then your for loop using a.at() from your code above.

EDIT: You threw in something as I posted the pseudo-code.

You can throw out a variable or two and do this:
1
2
3
4
5
6
7
8
9
// set spaces when user input for width is greater than string
if(width > a.length()) {
	for(int i = a.length + 1; i < width; i ++) 
		a.append(" ");	
}

// print to screen
for(int i = 0; i < a.length(); i++)
	cout << a.at(i)


The logic is correct, but you may need to tweak this to make sure it compiles.
Last edited on Apr 20, 2012 at 8:17pm
Apr 20, 2012 at 8:13pm
ill try that, but is there something wrong with this?
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	int ctr;
	int width;
	int x;
	string a;

	cout << "Enter a string: ";getline(cin, a);
	cout << "Enter the number of letters you want to see from that string: ";
	cin >> width;

	if(width <= a.length())
	{
		for(ctr = 0; ctr < width; ctr++)
		{
			cout << a.at(ctr);
		}
	}
	else
	{
		x = a.length();

		for(x; x < width; x++)
		{
			a.append(" ");
		}
		cout << a;
	}
	cout << "|";
	cin.get();
	cin.get();
	return 0;
}
Apr 20, 2012 at 8:21pm
We got out of sync, but I edited my reply above with more info.

Your method seems fine, just has some redundancy. How does it run? Sorry, not able to run it at the moment.

Note that the for-loop in the else block doesn't print one character at a time as I assume you intend.
Last edited on Apr 20, 2012 at 8:22pm
Apr 20, 2012 at 8:22pm
1
2
3
4
5
6
7
8
if ( ctr > a.length() ) // the conditional
{
    cout << " "; // out the space, you can use whichever form of space you want.
}
else
{
    cout << a.at(ctr);
}
Apr 20, 2012 at 8:25pm
What I came up with seems to be working fine, but I wouldn't have been able to do it without the append() function. Thank you very much for you help. Again though, I'm new so I'm not quite sure what you mean by redundancy. Could you explain please?
Apr 20, 2012 at 8:30pm
My code above your posted code shows the same method while taking advantage of re-using variables. That's really all I meant.

Glad it helped.
Apr 20, 2012 at 8:44pm
What you need to understand is that a string is a complex array of characters.

If you want element 2 of the string, then string[1] will return it as a character.

Append adds another element with the character you have in parenthesis.

The code he gave you will simply add spaces to the end of the original string if the number is larger than the strings length. After that, it displays the entire string in one go.

If you want to display one character at a time, use my code in the for loop of your original post.
Topic archived. No new replies allowed.