Compiler Error with Char

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.

Any help or adivce would be really appreciated :)

SwanSong
cUserName[i] = ' '

You need to use ' instead of " when using char
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??
can you upload the new loop please?
course... here's the full working code as it stands:

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
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    char cUserName[20];
    char cYearGrp[4];
    cout << "Please enter the Username you wish to ban: ";
    cin >> cUserName;
    cout << "\nPlease enter the users Intake Year: ";
    cin >> cYearGrp;
    
    int i = 0;
    while (i < 20) {
      if (!isalnum(cUserName[i]) || !ispunct(cUserName[i])) {
          cout << "value of i is: " << i;
          cout << "and its char value is: " << cUserName[i] << endl;
          i++;
      }
      else {
        cUserName[i] = ' ';
        break;
      }
    }
    cout << "The string is: " << cUserName << endl;
    strcat(cUserName, cYearGrp);
    
    system("PAUSE");
    //CreateProcess('cmd.exe', szCmdLine);
    return 0;    
}
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 >.> )
You dont updat i when char[i] is a space (if i understand your code well, i dont know isalnum() and ispunct())

It goes wrong with the first space... (and the first letter)
So...ur problem is that cYearGrp has the length 4 in stead of 5. Let's say year 2000....in a str it really is: {'2','0','0','0','\0'}.

By the way....I think u'd better init those 2 char arrays:
1
2
3
4

    char cUserName[20]="";
    char cYearGrp[4]="";

Last edited on
Scipio

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!

Thanks for the help guys :)

SwanSong
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??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

using namespace std;

int main() {
 string name = "";
 string date = "";
 cout << "Please enter name:";
 cin >> name;
 cout << "Please enter date:";
 cin >> date;

 cout << string(name+date) << endl;

 return 0;
}
Ah right, using String methods over Char... I'll have to give it a test :)
Topic archived. No new replies allowed.