Having an error when trying to pass a string value

In my program which I'm writing as assignment for my HND is giving me a an error when I enter data in to a string value. Below is my struct Array which I'm storing my data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct stdDetails {

   	int stdNum; //Student Registration Number
    char fName[25]; // Student First Name
    char lName[25]; // Student Last Name
	string resAdd; //Resident Address
	char dBirth[10]; // Student Date Of Birth
	int stdAge; // Student Age
	string nicNum; // Sudent National ID number
	int conNum; // Student Contact Number
    char dateEnroll[10]; // Student Enrollment date
	char corsSelect[25]; // Course Selected
	char payMethod; // Fee payment method
    double amtPaid; // Amount Student Paid for the course    
    char bksIssue[25]; //Issued Books

};


In the above struct I've 2 strings my problem is when I enter data when program is running it gives a error and closes I can't read the error.

Below where I enter data.

1
2
3
4
5
6
cout<<"Enter Student Resident Address : ";
	//getline(cin,stdDetailsStructs_1.resAdd);
	cin>>stdDetailsStructs_1.resAdd;

cout<<"Enter Student NIC number : ";
	cin>>stdDetailsStructs_1.nicNum;


Please help me to get this code right and to accept data in to the array
char str[60] is not really a string, it's an array of char that's used a string in C. But we can do better in C++.

In C++, use std::string. You'll need:
 
include <string>
Thank you very much for the quick reply but can u show me an example I'm new to the C++ coding that's why m asking so many questions.
An example is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

void getname(std::string& name)
{
    std::cout << "Please enter a name: ";
    std::cin >> name;
}

void putname(const std::string& name)
{
    std::cout << "Name is: " << name << std::endl;
}

int main()
{
    std::string n;
    getname(n);
    putname(n);

    return 0;
}
Last edited on
Thank you very much
Topic archived. No new replies allowed.