Multiple C++ questions

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
I have to do this program without classes so all replies, don't use classes. How do you make multiple use of multiple objects in the same function but at different times? Like have the first person enter something than have the second set of objects take the place of the first when a new set of information is to be inputted. Secondly, how do you find the amount of digits in a variable/object? I might have more questions so please help me out.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
- You need the clarifiy what you mean by the first question.
- For getting the number of digits, if the variable is a string, then just use strlen(), else the easiest way is to use a function like this:

// For positive integers only.
int CountDigits(int number)
{
int count = 0;
while(true)
{
number = number / 10;
count++;

if(number == 0)
return count;
}

}
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Okay, I have a structure that has 5 things, char lname, char fname, int idnumber, int pay, int hours. I have a function where you call that and later on the user enters whether they want to run the program again so the function that uses the structure has to change its object to be inputted in. Like the first time they enter stuff it's cin>>objEmp1.lname; but you can't change that. I hope you see what I mean.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: idNut
Okay, I have a structure that has 5 things, char lname, char fname, int idnumber, int pay, int hours. I have a function where you call that and later on the user enters whether they want to run the program again so the function that uses the structure has to change its object to be inputted in. Like the first time they enter stuff it's cin>>objEmp1.lname; but you can't change that. I hope you see what I mean.

I really don't. Consider this code fragment:

struct input
{
char strName[50];
int number;
};


void GetInput(input &in)
{
cout << "Name: ";
cin >> in.strName;

cout << "Number: ";
cin >> in.number;
}

void main()
{
input user_input;
GetInput(user_input);
GetInput(user_input);
GetInput(user_input);
}


What exactly do you mean by re-using?