for loop problem~!

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
/*

4-40 for Automobiles 
Use a forloop to get the information about each of a person’s automobiles: name, make, year, color. 
No need to retain the information, just show it back to the person.
*/

#include <iostream>
#include <string>
using namespace std;
int main()
{
 string name, make, color;
 double year, owners;

 cout << "I need your automobile information!\n";
 cout << "How many people in your family own automobiles?\n";
 cin >> owners;

 for (int n=1; n <= owners; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> name;
  
  cout << "What is his/her car's make: \n";
  cin >> make;

  cout << "What is his/her car's year: \n";
  cin >> year;

  cout << "What is his/her car's color: \n";
  cin >> color;

  cout << endl;
 }

  cout << name << "'s car make is " << make << "of year " << year << " and is " << color << ".\n";

  system ("pause");
}


Heey, this is my code and i wanted to replay back out each person's info in the cout << name << "'s car make is " << make << "of year " << year << " and is " << color << ".\n";

part, but i realized i kept overwriting the previous person's info. how would i keep it all?
Hope this helps.

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
55
56
/*

4-40 for Automobiles 
Use a forloop to get the information about each of a person’s automobiles: name, make, year, color. 
No need to retain the information, just show it back to the person.
*/

#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct info
{

 string name, make, color;
 double year;


};
int main()
{

 int owners=0;
 cout << "I need your automobile information!\n";
 cout << "How many people in your family own automobiles?\n";
 cin >> owners;

struct info *list = new struct info[owners];
 for (int n=0; n < owners; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> list[n].name;
  
  cout << "What is his/her car's make: \n";
  cin >> list[n].make;

  cout << "What is his/her car's year: \n";
  cin >> list[n].year;

  cout << "What is his/her car's color: \n";
  cin >> list[n].color;

  cout << endl;
 }
  for (int n=0; n <owners; n++)
 {
   cout << list[n].name << "'s car make is " << list[n].make << " of year " << list[n].year << " and is " << list[n].color << ".\n";
  }
delete []list;
cin.sync();
cout<<"Press Enter Key To Exit"<<endl;
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
return 0;
}

Last edited on

Subzero030201 (25) Mar 7, 2012 at 7:20pm
Hope this helps.


correct me if i'm wrong, but this is using class structures right? i barely learned class structures last night >.< so i'm going to try to learn some more right now!

can you explain what this part is?struct info *list = new struct info[owners];
and what does the list[n].blah mean ?

also, is there a way without using class structures? i'm fairly new at this.. :\
Yea you can use vectors.
The struct is simpler to manage your code.

Last edited on
Thats another way.
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

#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;

int main()
{
 vector<string>name	; vector<string>make;  vector<string>color;
 vector<double>year;
 double dtemp;
 string stemp;
 int owners=0;
 cout << "I need your automobile information!\n";
 cout << "How many people in your family own automobiles?\n";
 cin >> owners;





 for (int n=0; n < owners; n++)
 {
  cout << "What is the owner's name: \n";
  cin >>stemp;
  name.push_back(stemp);
  cout << "What is his/her car's make: \n";
  cin >> stemp;
  make.push_back(stemp);
  cout << "What is his/her car's year: \n";
  cin >> dtemp;
  year.push_back(dtemp);
  cout << "What is his/her car's color: \n";
  cin >> stemp;
  color.push_back(stemp);
  cout << endl;
 }
  for (int n=0; n < owners; n++)
 {
   cout << name[n] << "'s car make is " << make[n] << " of year " << year[n] << " and is " << color[n] << ".\n";
  }

cin.sync();
cout<<"Press Enter Key To Exit"<<endl;
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
}
Last edited on
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
 string info [3][3];
 int time[3];
 int year;
 string name, make, color;
 cout << "I need your automobile information!\n";
 cout << "Give me 3 people's information!\n";

 for (int n=1; n <= 3; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> name;
  
  cout << "What is his/her car's make: \n";
  cin >> make;

  cout << "What is his/her car's year: \n";
  cin >> year;

  cout << "What is his/her car's color: \n";
  cin >> color;

  cout << endl;
 }

  cout << info [1][1] << "'s car make is " << info [1][2] << "of year " << time[1] << " and is " << info [1][3] << ".\n";
  cout << info [2][1] << "'s car make is " << info [2][2] << "of year " << time[2] << " and is " << info [2][3] << ".\n";
  cout << info [3][1] << "'s car make is " << info [3][2] << "of year " << time[3] << " and is " << info [3][3] << ".\n";

  system ("pause");
}


okaay, this is my new code using arrays... i think its right D: but when i run it, something really weird happens! and subzero, what does the include limits mean? i havent learned vectors yet.. so i shall read up on it!
It allows some functions that allow you to be able to pause the screen.It is better than system("pause");. System is thought to be by many truly evil.
Last edited on
OHH. i see i see! i'll just use system pause for these small homework assignments for now. :D

do you mind trying to run my code? i get this realllyyy weird thing near the end!
Valid indexes for arrays are 0 to size-1. Not 1 to size.
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
 string info [2][2];
 int time[2];
 int year;
 string name, make, color;
 cout << "I need your automobile information!\n";
 cout << "Give me 3 people's information!\n";

 for (int n=1; n <= 3; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> name;
  
  cout << "What is his/her car's make: \n";
  cin >> make;

  cout << "What is his/her car's year: \n";
  cin >> year;

  cout << "What is his/her car's color: \n";
  cin >> color;

  cout << endl;
 }

  cout << info [0][0] << "'s car make is " << info [0][1] << " of year " << time[0] << " and is " << info [0][2] << ".\n";
  cout << info [1][0] << "'s car make is " << info [1][1] << " of year " << time[1] << " and is " << info [1][2] << ".\n";
  cout << info [2][0] << "'s car make is " << info [2][1] << " of year " << time[2] << " and is " << info [2][2] << ".\n";

  system ("pause");
}


here is my editted code! but it's still not working out! D: does anyone see a mistake??
give it a try... Start learning vectors they are a whole lot better to work with.
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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string info [3][3]=
				{
				{"random","random","random"},
				{"random","random","random"},
				{"random","random","random"}
				};
 int time[3]={1,1,2};
 int year;
 string name, make, color;
 cout << "I need your automobile information!\n";
 cout << "Give me 3 people's information!\n";

 for (int n=0; n < 3; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> name;
  info [n][0]=name;
  cout << "What is his/her car's make: \n";
  cin >> make;
  info [n][1]=make;
  cout << "What is his/her car's year: \n";
  cin >> year;
  time[n]=year;
  cout << "What is his/her car's color: \n";
  cin >> color;
  info [n][2]=color;
  cout << endl;
 }

  cout << info [0][0] << "'s car make is " << info [0][1] << " of year " << time[0] << " and is " << info [0][2] << ".\n";
  cout << info [1][0] << "'s car make is " << info [1][1] << " of year " << time[1] << " and is " << info [1][2] << ".\n";
  cout << info [2][0] << "'s car make is " << info [2][1] << " of year " << time[2] << " and is " << info [2][2] << ".\n";

  system ("pause");
}


Last edited on
here is my editted code! but it's still not working out! D: does anyone see a mistake??


1
2
 string info [2][2];
 int time[2];


1
2
3
cout << info [0][0] << "'s car make is " << info [0][1] << " of year " << time[0] << " and is " << info [0][2] << ".\n";
  cout << info [1][0] << "'s car make is " << info [1][1] << " of year " << time[1] << " and is " << info [1][2] << ".\n";
  cout << info [2][0] << "'s car make is " << info [2][1] << " of year " << time[2] << " and is " << info [2][2] << ".\n";


I say again:
Valid indexes for arrays are 0 to size-1.


Also, info contains junk. You never put anything into it.
cire, what do you mean by 0 to size - 1? i have 3 variables so wouldnt it be 0, 1, 2?

and, how would i put data into info?

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
 string info [2][2] = {
	 {"name", "make", "color"},
	 {"name", "make", "color"}, 
	 {"name", "make", "color"}
						};
 int time[2] = {0, 0, 0};
 int year;
 string name, make, color;
 cout << "I need your automobile information!\n";
 cout << "Give me 3 people's information!\n";

 for (int n=1; n <= 3; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> name;
  
  cout << "What is his/her car's make: \n";
  cin >> make;

  cout << "What is his/her car's year: \n";
  cin >> year;

  cout << "What is his/her car's color: \n";
  cin >> color;

  cout << endl;
 }

  cout << info [0][0] << "'s car make is " << info [0][1] << " of year " << time[0] << " and is " << info [0][2] << ".\n";
  cout << info [1][0] << "'s car make is " << info [1][1] << " of year " << time[1] << " and is " << info [1][2] << ".\n";
  cout << info [2][0] << "'s car make is " << info [2][1] << " of year " << time[2] << " and is " << info [2][2] << ".\n";

  system ("pause");
}
cire, what do you mean by 0 to size - 1? i have 3 variables so wouldnt it be 0, 1, 2?


string info [2][2];

I quoted your code for a reason. You don't have room for 3. So size is 2, and 0 to size-1 is 0 to 1.


and, how would i put data into info?


The same way you put data into other variables.

Last edited on
cire (231) Mar 8, 2012 at 11:25pm
cire, what do you mean by 0 to size - 1? i have 3 variables so wouldnt it be 0, 1, 2?


string info [2][2];

I quoted your code for a reason. You don't have room for 3. So size is 2, and 0 to size-1 is 0 to 1.


and, how would i put data into info?


The same way you put data into other variables.


gahh. i am at a lost on how to insert data into arrays. sorry.. my teacher hasnt taught me any of this yet and bleeh. cire, would i change the numbers to 4 then? cause 4-1 is 3... O.O *scratches head* sorry... i'm fairly new . :(

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
 string info [4][4] = {
	 {"name", "make", "color"},
	 {"name", "make", "color"}, 
	 {"name", "make", "color"}
						};
 int time[] = {0, 0, 0};

 cout << "I need your automobile information!\n";
 cout << "Give me 3 people's information!\n";

 for (int n=1; n <= 3; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> info;
  
  cout << "What is his/her car's make: \n";
  cin >> info;

  cout << "What is his/her car's year: \n";
  cin >> time;

  cout << "What is his/her car's color: \n";
  cin >> info;

  cout << endl;
 }

  cout << info [1][1] << "'s car make is " << info [1][2] << " of year " << time[1] << " and is " << info [1][3] << ".\n";
  cout << info [2][1] << "'s car make is " << info [2][2] << " of year " << time[2] << " and is " << info [2][3] << ".\n";
  cout << info [3][1] << "'s car make is " << info [3][2] << " of year " << time[3] << " and is " << info [3][3] << ".\n";

  system ("pause");
}
magadavixt wrote:
gahh. i am at a lost on how to insert data into arrays

One-dimensional 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
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main()
{
   //Declaring an array of 3 strings
   //The valid indexes for this array are 0, 1, and 2
   //for the first, second, and third element, respectively
   string arrayOfStrings[3];

   //at this point, each string is the empty string
   //as that's what the string is set to when its
   //default constructor is called.

   //alternatively, we could have initialized the
   //array like the following, giving each element
   //an initial value:
   // string arrayOfStrings[3] = {"one", "two", "three"};

   //assigning "first" to the first element
   arrayOfStrings[0] = "first";

   //assigning "second" to the second element
   arrayOfStrings[1] = "second";

   //getting the user to input the third string
   cout << "Please enter the third string: " << endl;
   getline(cin, arrayOfStrings[2]);

   //we used getline instead of cin >> so that we could enter spaces into the string

   //outputting the results
   for(int i = 0; i < 3; i++)
   {
      cout << "string number " << i + 1 << ": " << arrayOfStrings[i] << endl;
   }

   cout << "Press ENTER to continue ..." << endl;
   cin.ignore(numeric_limits<streamsize>::max(), '\n'); //proper way to keep the console open

   return 0;
}


Output:

Please enter the third string:
a bunch of stuff
string number 1: first
string number 2: second
string number 3: a bunch of stuff
Press ENTER to continue ...


Two-dimensional 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 <iostream>
#include <string>
#include <limits>
using namespace std;

int main()
{
   //Declaring a 2 by 3 array of strings. Essentially, here
   //we have two arrays where each array stores an
   //array with 3 strings in it.
   string twoByThreeArrayOfStrings[2][3];

   //we access the 2D array as: twoByThreeArrayOfStrings[i][j]
   //where i is the first index into the array (i = 0 would mean
   //we want the first array of 3 strings and i = 1 would mean
   //we want the second array of 3 strings) and j is the second
   //index into the array (i = 0, j = 0 means we want the first 
   //string (j = 0) of the first array (i = 0). i = 1, j = 2 means we
   //want the third string (j = 2) of the second array (i = 1)).

   //i can be either 0 or 1 and j can be either 0, 1, or 2 in this case.
   //Rule: Given that an array is of size N, then valid indexes are
   //{0, 1, 2, ..., N - 1}. In the one-dimensional example above, N
   //is 3 so the valid indexes were 0, 1, and 2.

   //at this point, all the strings are the empty string. Let's ask
   //the user to fill them in.
   for(int i = 0; i < 2; i++)
   {
      for(int j = 0; j < 3; j++)
      {
         cout << "Please enter string number " << j + 1 << " of array number " << i + 1 << endl;
         getline(cin, twoByThreeArrayOfStrings[i][j]);
      }
      cout << endl;
   }
   cout << endl;

   //let's repeat what the user put into our array
   for(int i = 0; i < 2; i++)
   {
      for(int j = 0; j < 3; j++)
      {
         cout << "User entered '" << twoByThreeArrayOfStrings[i][j] << "' for string number " << j + 1 << " of array number " << i + 1 << endl;
      }
      cout << endl;
   }
   cout << endl;

   cout << "Press ENTER to continue ..." << endl;
   cin.ignore(numeric_limits<streamsize>::max(), '\n'); //proper way to keep the console open

   return 0;
}


Output:

Please enter string number 1 of array number 1
first string
Please enter string number 2 of array number 1
hello
Please enter string number 3 of array number 1
OK

Please enter string number 1 of array number 2
how many more do I have to enter?
Please enter string number 2 of array number 2
that didn't answer my question'
Please enter string number 3 of array number 2
fine then :p


User entered 'first string' for string number 1 of array number 1
User entered 'hello' for string number 2 of array number 1
User entered 'OK' for string number 3 of array number 1

User entered 'how many more do I have to enter?' for string number 1 of array number 2
User entered 'that didn't answer my question'' for string number 2 of array number 2
User entered 'fine then :p' for string number 3 of array number 2


Press ENTER to continue ...
Last edited on
ok real simple it goes size-1 ... say myarray[3] that is three 0,1,2 which is three spots.

myarray[0] spot 1.->good
myarray[1] spot 2.->good
myarray[2] spot 3.->good
myarray[3] spot 4 -> exceeding bounds of array.

... I wrote the code already.

If you are still having problems.
http://www.cplusplus.com/doc/tutorial/arrays/

next your problem. you have to store your strings and int into your arrays.
Again look at my code.


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


#include <iostream>
#include <string>
using namespace std;
int main()
{
	string info [3][3]=
				{
				{"random","random","random"},
				{"random","random","random"},
				{"random","random","random"}
				};
 int time[3]={1,1,2};
 int year;
 string name, make, color;
 cout << "I need your automobile information!\n";
 cout << "Give me 3 people's information!\n";

 for (int n=0; n < 3; n++)
 {
  cout << "What is the owner's name: \n";
  cin >> name;
//got to store string into array
  info [n][0]=name;
  cout << "What is his/her car's make: \n";
  cin >> make;
//got to store string into array
  info [n][1]=make;
  cout << "What is his/her car's year: \n";
  cin >> year;
//got to store int into array
  time[n]=year;
  cout << "What is his/her car's color: \n";
  cin >> color;
//got to store string into array
  info [n][2]=color;
  cout << endl;
 }

  cout << info [0][0] << "'s car make is " << info [0][1] << " of year " << time[0] << " and is " << info [0][2] << ".\n";
  cout << info [1][0] << "'s car make is " << info [1][1] << " of year " << time[1] << " and is " << info [1][2] << ".\n";
  cout << info [2][0] << "'s car make is " << info [2][1] << " of year " << time[2] << " and is " << info [2][2] << ".\n";

  system ("pause");
}


//  --- lh bw ---


Last edited on
How would you do this for loop if you wanted to ask the user how many cars he/she has and then set the loop according to that number?
So if the user has 5 cars, then the loop will ask the same set of questions about each car 5 times.
make a struct... or make them vectors ... i believe i did that already in this thread if you search the code above.


Here is one of the ways using a struct.
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
55
56
/*
/*

4-40 for Automobiles 
Use a forloop to get the information about each of a person’s automobiles: name, make, year, color. 
No need to retain the information, just show it back to the person.
*/

#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct info
{

 string name, make, color;
 double year;


};
int main()
{

 int owners=0;
 cout << "I need your automobile information!\n";
 cout << "How many people in your family own automobiles?\n";
 cin >> owners;
 cin.ignore();
struct info *list = new struct info[owners];
 for (int n=0; n < owners; n++)
 {
  cout << "What is the owner's name: ";
  getline(cin,list[n].name);
  
  cout << "What is his/her car's make: ";
  getline(cin,list[n].make);

  cout << "What is his/her car's year: ";
  cin >> list[n].year;
  cin.ignore();
  cout << "What is his/her car's color: ";
  getline(cin,list[n].color);

  cout << endl;
 }
  for (int n=0; n <owners; n++)
 {
   cout << list[n].name << "'s car make is " << list[n].make << " of year " << list[n].year << " and is " << list[n].color << ".\n";
  }
delete []list;
cin.clear();
cin.sync();
cout<<"Press Enter Key To Exit"<<endl;
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
return 0;
}
Last edited on
Topic archived. No new replies allowed.