Stuct problem

I get an error anytime I try to put in a value for a variable in rAccount. error message is:

"main.cpp: In function 'rAccount reg()':
main.cpp:65:16: error: expected primary-expression before 'rAccount'
cin >> string rAccount.name;
^
main.cpp:65:16: error: expected ';' before 'rAccount'
main.cpp:66:10: error: expected unqualified-id before '.' token
rAccount.actNum = rand() % 100000 + 999999;
^
main.cpp:67:10: error: expected unqualified-id before '.' token
rAccount.actBal = 0.0;"

code is as follows:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

struct rAccount
{
string name;
int actNum;
float actBal;
};

int main()
{
reg();
return 0;
}

rAccount reg()
{
cout << "Enter your name: ";
cin >> rAccount.name;
rAccount.actNum = rand() % 100000 + 999999;
rAccount.actBal = 0.0;
cout << endl << "Your account details are: " << endl;
//cout << reg();
}
Last edited on
rAccount is the type. It is not an identifier (name of an instance of that class).
So, you when you try to access rAccount.name, you're not actually accessing the member of any instance of that class. Also, your reg() function expects to return an rAccount object, but it doesn't return anything.
See here string name; where you've created a `string' named `name'.
You need to create an object of type `rAccount' and operate on it.
1
2
rAccount an_object_with_a_meaningful_name;
cin >> an_object_with_a_meaningful_name.name;
Thank you very much
Topic archived. No new replies allowed.