C-Syle String won't clear

I built a program that asks the user to input a string of characters. It then assigns the program to that string using cin.get(x,x). It goes about doing what I want until the end. I want it to repeat the program but when it does it just spits out part of the inputs from the previous run.
I figured it's because the string still had some of the characters stored. So I'm trying to clear the string but when I use commands like:
stringname.clear
or something similar i get the error:
error C2227

Any help would be aprreciated, it's a C style string.
Last edited on
Source code pls
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


#include <cmath> 
#include <iostream>
#include <string>
#include <vector>
using namespace std;
char getinput();
const int n = 11;
char sentence[10];
char cStringToUpper();
void ask();
char ans;

char getinput()
{
	cout <<"Enter a string with no spaces\n" << endl;
	cin.get(sentence, n + 1);
	return 0;

}

char cStringToUpper()
{
	
	int i = 0;
	for (i = 0; i < 11; i++)
	{
		int val = sentence[i];
		if (val > 90)
		{
			sentence[i] = val - 32;
		}
		cout << sentence[i];
		
	}
	
	
	cout << "\n" << endl;
	
	return 0;
	
}

void ask()
{
	cout << "Enter Q to quit or anything else to continue \n";
	cin >> ans;
	
	
}





int main( )
{
	getinput();
	cStringToUpper();
	ask();
	sentence.clear();

	if (ans != 'Q')
	{
	getinput();
	cStringToUpper();
	ask();
	}
	
	
	

cin.ignore(2);
return 0;
}


Error-
cpp(62) : error C2228: left of '.clear' must have class/struct/union

Without clearing the string it tends to repeat letters because i believe they're still stored in the memory. I want to clear it.

Here's what I get out-

CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
Enter a string with no spaces

abcdefg
ABCDEFG

Enter Q to quit or anything else to continue

d
Enter a string with no spaces

BCDEFG D

Enter Q to quit or anything else to continue
Press any key to continue . . .


The thing is I think I know the problem and I know I know how to fix the problem, I just can't put my finger on it. I'm thinking it could be that it's submitting everything typed to memory and won't clear it. Because if you enter many characters it'll automatically reloop the program and display those
for example:


UNC paths are not supported. Defaulting to Windows directory.
Enter a string with no spaces

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJK

Enter Q to quit or anything else to continue
Enter a string with no spaces

MNOPQRSTUVW

Enter Q to quit or anything else to continue
Press any key to continue . . .


Last edited on
sentence is a char array, not a string object that can access clear().

1
2
3
4
5
string sentence;

// do stuff

sentence.clear();
Anyway to clear a char array or fix the problem I'm experiencing?
Last edited on
Fill the array with '\0' characters
Use a loop to do that I assume, no easier way right?

I'll give it a shot.
Alright that seems to work, but here's a problem I've never come across. When I say if the user enters Q it'll quit, as expected, but if the user enters anything else to continue the program inputs that as the first letter in the next string. It also bypasses the function which clears the array.

I think they're related problems....It's odd...
I think you should call cin.sync() and on your loop in cStringToUpper() the condition should be i < 10.
The way you maid the main() will produce only 2 repetitions of the input, I suggest you changing the if with a while
Last edited on
wait replace cin.get with cin.sync?
just trying to see if I got what you're saying.
I'll try the other things.

edit-
I implemented the while and realized why I changed it to if in the beginning.
With a while loop it sends the program into an infinite loop.
Last edited on
No calling cin.sync() to clear the buffer.
The while works:
1
2
3
4
5
6
7
8
9
while (ans != 'Q')
{
     getinput();
     cStringToUpper();
     for (int i=0; i<10; i++)
          sentence[i]=0;
     ask();
     cin.sync();
}
Last edited on
Alright I got it to stop looping.
cin.sync did the trick but I had to put it in both places, naturally.

But
the letter I enter that's not Q shows up in the next string automatically.
Last edited on
You don't need anything before the loop in main function
In your loop in cStringToUpper() the condition should be i < 10.
For sync() reference, read http://www.cplusplus.com/reference/iostream/istream/sync.html
Last edited on
The letter I enter continues to carry over to the next string...

example:

Enter a string with no spaces

abc
ABC

Enter Q to quit or anything else to continue
Z
Enter a string with no spaces

abc
ABC Z

Enter Q to quit or anything else to continue


I'm putting cin.sync in different places to see if I can get rid of the letter input.
Last edited on
Thats because the condition in cStringToUpper (as I told you already twice...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char cStringToUpper()
{	
	int i = 0;
	for (i = 0; i < 11/*Here should be 10, not 11*/; i++)
	{
		int val = sentence[i];
		if (val > 90)
		{
			sentence[i] = val - 32;
		}
		cout << sentence[i];		
	}
		
	cout << "\n" << endl;	
	return 0;	
}
Last edited on
I was about to say I was but I realized that after I changed it I hit ctrl z to change something back and in the process changing that back.

Sry, code works perfect now
Thank You very much for the help.
Thanks even more for the patience.

Topic archived. No new replies allowed.