I am working on a project that takes in a Name and a Year group to run functions on, however I keep on getting a compiler crash in one statement of my code. It should take in the username via cin and then run an iterater until the first none-alpha character is found and set it to a Space. This is so it can be concatenated with another char variable later in the script.
Line 9 is what is giving me the error.
1 2 3 4 5 6 7 8 9 10 11 12
char cUserName[20];
cin >> cUserName;
int i = 0;
while (i < 20) {
if (!isalpha(cUserName[i]) {
i++
}
else {
cUserName[i] = " ";
break;
}
}
Its giving me an error of "Could not convert const char* to char". This is on a Win32 system running GCC via MinGW.
yep that solved that!! But now the program spits out garbage! I modified the loop slightly to se what happened at got this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Please enter the Username you wish to ban: Jamie
value of i is: 0and its char value is:
value of i is: 1and its char value is: a
value of i is: 2and its char value is: m
value of i is: 3and its char value is: i
value of i is: 4and its char value is: e
value of i is: 5and its char value is:
value of i is: 6and its char value is: ┴
value of i is: 7and its char value is: w
value of i is: 8and its char value is:
value of i is: 9and its char value is:
value of i is: 10and its char value is:
value of i is: 11and its char value is:
value of i is: 12and its char value is: )
value of i is: 13and its char value is: N
Anyone have any idea what could be causing this kinda of output??
Its a pattern that seems to repeat, just tried it with nick as the input instead and that gave pretty much exactly the same:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Please enter the Username you wish to ban: nick
Please enter the users Intake Year: 2004
value of i is: 0and its char value is:
value of i is: 1and its char value is: i
value of i is: 2and its char value is: c
value of i is: 3and its char value is: k
value of i is: 4and its char value is:
value of i is: 5and its char value is: (
value of i is: 6and its char value is: ┴
value of i is: 7and its char value is: w
value of i is: 8and its char value is:
value of i is: 9and its char value is:
value of i is: 10and its char value is:
value of i is: 11and its char value is:
value of i is: 12and its char value is: )
value of i is: 13and its char value is: N
I could understand this if the char cUserName[20]; was uninitated and contained garbage, but as its not being decl'd or new'd I'm not sure why this would output as it does (or why it breaks down before 19 >.> )
Right I see... the functions are not properly picking up what I am asking it. My idea is this:
1 2 3 4
Normal Code
Input: Jamie
Input: 2004
Strcat: Jamie2004
1 2 3 4 5
My Code
Input: Jamie
Input: 2004
Loop
Strcat: Jamie 2004
Because the loop is doing what it is supposed to be it sets my first value cUserName[i] to a space... probably caused by it not properly being initiated.
Smoke006
Yeah that probably didn't help :)
When I initiated the string as you suggested it worked... I was working under the assumption that it would initiate anyway unless I specified something like new or decl (I've come out of PHP) but that obviously isn't the case!
Do you have to use C conventions for your project? Because you have potential buffer-overflow there that would cause your app to crash and allow potential un-authorized code to be executed.
Not especially if I understand what you mean... for instance if a user tries to type in more than can be handled by my cUserName and cYearGrp variables?
How would you suggest preventing this kind of situation??