needs some help /advice


might be a simple fix but i dont see it and its drving me nuts
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

class cardata4{

public:


int getAge();
void setAge(int newValue);
void setName (string model);
string getName();


private:
int age;
string name ;


};



string cardata4::getName(){

return name;

}


int cardata4::getAge(){

return age;

}

void cardata4::setAge(int newValue){

age = newValue;

}
void setName (string model);
string model;
int i;
int newValue;
long inv;
int main()
{

cardata4 car[i];
do {
cout<< "how many cars would you like to add"<<endl;
cin>> inv;



for (i=1;i<= inv;i++)
{
setName(model);

};
for (i=1; i<= inv; i++)
cout << "car model number " << car[i].getName() << " is " << car[i].getAge() << " years old!\n";
break;
}
while ((inv > 1 ) || (inv >=9999));
cout<< "goodbye" <<endl;
system("pause");
return 0;
}
void setName(string model){

cout << "what is the name" <<endl;
cin >> car[i].name;

}
Move this to beginner's, use code tags, and actually state what your problem is.
I agree it is important to use code tags, and have it in the proper forum. Still no reason to avoid giving pointers on the code (like the fact that he has two functions named setName (granted one is a class method), bad habit to have for such a simple program). I simply thew this together to show a possible fix for it. Not the best, but figured since he hasn't moved the thread, nor replied back or put code tags, then I just did a basic example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cstdlib>
#include <string>
#include <iostream>

class carData4
{
		int age;
		std::string name;
	public:
		void setAge(int newValue);
		void setName(std::string model);
		int getAge()const { return age; }
		std::string getName()const { return name; }
};

void carData4::setAge(int newValue)
{
	age = newValue;
}

void carData4::setName(std::string model)
{
	name = model;
}

int main()
{
	std::cout << "How many cars do you need to enter?: ";
	int num;
	std::cin >> num;
	
	carData4 carList[num];
	std::string name;
	int year;
		
	for(auto i = 0; i < num; i++)
	{
		std::cout << "Enter the name: ";
		std::cin >> name;
		std::cout << "Enter the year: ";
		std::cin >> year;
		
		carList[i].setName(name);
		carList[i].setAge(year);
	}

	std::cout << "Just to check...\nYour entered...\nModel\tYear\n\n";
	for(auto i = 0; i < num; i++)
	{
		std::cout << carList[i].getName() << "\t" << carList[i].getAge() << std::endl;
	}
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.