0

I am trying to create a program where it will read in the student name, nationality, and grades, and calculate their tuition fee based on those information.

I began to write a bit of code by I have ran into some problems. When I try to run my code, the compiler says "warning C4700: local variable 'name' used without having been initialized" but continues to run the code. Then the program shuts down. What is happening?

In addition, on my fifth line I have "char *firstName;". I know that char* represents a pointer, but what does that mean and why can't I just use a string?

#include <iostream>
using namespace std;

struct Student {
	char *firstName;
	char *lastName;
	bool canadianship;
	int grades[10];
};

Student newStudent[10];

void addFirstName() {
	char *name;
	cout << "Student Name: ";
	cin >> name;
	newStudent[0].firstName = name;
}


int main() {
	addFirstName();
	cout << newStudent[0].firstName;	
	return 0;
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by icu222much
0

can't I just use a string?

Yes you can :

#include <iostream>
#include <string>

using namespace std;

struct Student {
	string firstName;
	string lastName;
	bool canadianship;
	int grades[10];
};

Student newStudent[10];

void addFirstName() {
	string name;
	cout << "Student Name: ";
	cin >> name;
	newStudent[0].firstName = name;
}


int main() {
	addFirstName();
	cout << newStudent[0].firstName;	
	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.