Using a char array

So I am working on this program for class to use a struct to make a student record with assigned values and then generalize the program so the user can enter their own data. I usually use Code:Blocks just because I'm used to it and my program runs fine on there. However, I ran it on Visual C++ 2010 and it will not run. The program specifies that a character array must be used (so no I cannot change it to a string even though that makes much more sense). Am I formatting the char array wrong? I looked up other examples of char arrays online and haven't been able to find why it is not working on Visual. Thanks in advanced for your help!

#include <iostream>
using namespace std;

struct StudentRecord
{
char Name[20];
int ID;
float GPA;
};

int main()
{
StudentRecord TESCStudent;

TESCStudent.Name = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r'};
TESCStudent.ID = 1234;
TESCStudent.GPA = 4.0;

cout << "Student Name: " << TESCStudent.Name << "\n";
cout << "ID Number: " << TESCStudent.ID << "\n";
cout << "GPA: " << TESCStudent.GPA << "\n\n";

cout << "Enter your personal information:\n";
cout << "Enter name as character string:";
cin >> TESCStudent.Name;
cout << "Enter ID Number:";
cin >> TESCStudent.ID;
cout << "Enter GPA:";
cin >> TESCStudent.GPA;

cout << "\nYour student information:\n\n";
cout << "Name: " << TESCStudent.Name << "\n";
cout << "ID: " << TESCStudent.ID << "\n";
cout << "GPA: " << TESCStudent.GPA << "\n";

return 0;

}
Last edited on
Show the exact error message and what statement the compiler says is wrong.
Sorry, forgot about that. It doesn't really say which part is wrong but { was only used in the struct and the char array and im pretty sure its the char array.

1>------ Build started: Project: derpyy, Configuration: Debug Win32 ------
1> derpyy.cpp
1>c:\users\always broken\documents\visual studio 2010\projects\derpyy\derpyy\derpyy.cpp(19): error C2059: syntax error : '{'
1>c:\users\always broken\documents\visual studio 2010\projects\derpyy\derpyy\derpyy.cpp(19): error C2143: syntax error : missing ';' before '{'
1>c:\users\always broken\documents\visual studio 2010\projects\derpyy\derpyy\derpyy.cpp(19): error C2143: syntax error : missing ';' before '}'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

U probably forgot an = sign...but this wont work anyway as C style arrays cannot be assigned to. Don't forget that null char '\0' !
TESCStudent.Name{'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r'};

U could do something like this:
1
2
3
4
char cmyName[] = "superprogrammer"; //null char is in there too
memcpy(TESCStudent.Name, cmyName, sizeof(cmyName));
TESCStudent.ID = 1234;
TESCStudent.GPA = 4.0;
Last edited on
Use the assignment operator

TESCStudent.Name = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r'};
Oh right, I have that right in my program I just must have deleted it when I was pasting it. It still won't work for some reason. (I'm editing it above to fix the assignment operator error)
Well, you can not do this in MS VC++ 2010. But you can substitute the following two statements

1
2
3
StudentRecord TESCStudent;
 
TESCStudent.Name = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r'};

for one statement

StudentRecord TESCStudent = { 'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r' };
Tried that too. Doesn't work. Wish I could use a string lol.
You are wrong. It should work that is the member Name of the structure will be initialized and compiler will not issue any error. If you got an error then show it.
You are right, I forgot to delete the .Name part. However, If i change it to what you are saying I cannot also use the same thing for ID and GPA.
Why do you can not?
You can write either

1
2
3
StudentRecord TESCStudent = { 'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r' };
TESCStudent.ID = 1234;
TESCStudent.GPA = 4.0;


or

StudentRecord TESCStudent = { { 'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r' }, 1234, 4.0 };
Yay! You are right. No errors. Thanks for all the input, I am pretty new to C++.
Topic archived. No new replies allowed.