-
Structures Help
i have a program at the moment that is asking the user to input thier name, age and course. the details are stored in a stucture and is the passed to the function by the caller. the function collects the data and then on the return to the caller the stucture contains the useres details. then main() should output the contents of the structure.
at the moment i have this but for some reason when you put your name in it skips Age, and Course. also i am unsure if it is doing what i have explained corectly. this is what i have so far
Code:
#include <iostream>
using namespace std;
struct myStruct
{
char Name;
int age;
char course;
};
void myFunc1(myStruct m) // by value
{
cout<<"Enter you name";
cin >>m.Name;
}
void myFunc2(myStruct &m) // by reference
{
cout<<"Age: ";
cin>>m.age;
}
void myFunc3(myStruct &m)
{
cout<<"Course: ";
cin >>m.course;
}
int main()
{
myStruct m;
m.Name;
m.age;
m.course;
myFunc1(m);
myFunc2(m);
myFunc3(m);
system("pause");
}
any help will be much appreciated
-
i havent looked at the rest of the code but right off the bat i see this:
Code:
struct myStruct
{
char Name;
int age;
char course;
};
what datatype is Name? what values is it cabable of holding? what value are you intending to save in it? here lies your problem
edit: after looking at the rest of the code...
Code:
m.Name;
m.age;
m.course;
these lines dont do anythign so get rid of them. also, be consistent with your naming, make 'Name' lowercase 'name'.
-
how do you add datatypes to the name and the values of what it can hold?
-
there are 2 important points in this, scan through to find the relevant points or read it all, it wouldnt hurt.http://www.cplusplus.com/doc/tutorial/variables.html
-
A char holds a single character. You want a string to hold a string of characters. Use the C++ string class in <string> as you have in earlier programs.
-
hey managed to figure that bit out well i think i have, this is what i have got.
Code:
const int NAME_LEN = 10;
const int C_LEN = 20;
const int AGE = 10;
struct myStruct
{
char name[NAME_LEN];
int age[AGE];
char course[C_LEN];
};
just now need a bit of help to output it all in the way it was explained in the first post.
-
as daved said, you want a 'string'. use my link, and maybe your browsers search function and the word 'string' to figure it out, its on there!
the way you have it will work but is very susceptible to errors. your 'age' was fine how you initially had it, just name and course can be better.
-
do i actually need the amount of functions i have or would it be better using less?
-
if you are just learning structures then dont worry to much about efficiency like that.
did you solve the string problem? as said previously, youll need to #include <string> and change the 2 char variables to 'string's..
-
i have included the <string> but trying to get the string bit working, but not happening at the moment
-
post your updated code and let us know 'what isnt working' and we'll help
-
this is the example of the type of string i am trying to use
Code:
char stringname[10];
cout << "First Name: ";
cin.getline(stringname, 10);
cout << stringname << endl;
is this correct?
-
if you included string then declare the variables as strings instead of char[]. then just use: cin >> myString; to read input. if you need to save spaces i think you have to use: getline(cin,myString);
edit: yes that does look correct. but it would be easier to use 'string' variables because they are easier. no need to worry about out of bounds or indexes, etc, also the string class has many useful functions.
-
this is my new string code is it the same or better?
Code:
string mystring;
cout << "First Name: ";
cin>>mystring;
cout << mystring << endl;
if it is what is next to do to get it all working correctly?
-
this is what i have done to the first bit of the program using a sting, can you let me know if it is right or not, if not, what i can do to correct it?
Code:
#include <iostream>
#include <string>
using namespace std;
const int NAME_LEN = 10;
const int C_LEN = 20;
struct myStruct
{
string name[NAME_LEN];
int age;
char course[C_LEN];
};
void myFunc1(myStruct m) // by value
{
cout << "First Name: ";
cin >> m.name;
}