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 . . .