Why can't I use a for loop here?

It returns errors when I set up a for loop like I did below. It says error expected a declaration. Is there any reason for this? Is there any possible way I can set up a for loop in the way I have? Thanks.
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
#include "hw.h"
#include <string>
#include <iostream>
using namespace std;

bstore::bstore(){
}
int j;
for(j=0;j++;j<8){ //this line
string bstore::title()
{
	cout<<"Title of book: ";
	cin>> name;
	return name;
}
string bstore::author()
{ 
	cout<<"Name of Author: "<<endl;
	cin>> name;
	return name;
}
float bstore::price()
{ 
	cout<<"Cost of Book: "<<endl;
	cin>> money;
	return money;
}
int bstore::Count()
{ 
	cout<<"# of book: "<<endl;
	cin >> count;
	return count;
}
}
Last edited on
You can only place for loops inside functions.

1
2
3
4
5
6
7
8
for(...){}   // invalid

int main()
{
  for(...){}  //valid

  return 0;
}


And.... for(j=0;j++;j<8) is in the wrong order.
is should be:
for(j=0;j<8;j++)
Last edited on
so what would be the best way to fix this? I am in my .cpp for definitions so I dont think I can jsut make a function to hold everything, can I?
What are you trying to do with your loop? It seems to me like you're trying to make an array of books.
Last edited on
I want to run all of those functions 8 times and then store them in my array. As long as I can get them to loop I know how to store them in an array.

This way I have 8 books with info of title, author, cost and count.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
  string title;
  string name;
  float price;
  int count;
  
  bstore s;

  for(int = 0;i<8;i++)
  {
    title = s.title();
    name = s.author();
    price = s.price();
    count = s.Count();
  }
}


But maybe you should rethink how you wanna solve this.

Think of it like you have 1 shelf and 8 books which go on the shelf....
Last edited on
I tried doing it the way you show in the above comment previously. It lets my input values for the first iteration of the loop but after that it just flys through the rest without allowing me to input anything for the next 7 books. Any suggestions?
When trying it the way you show Jikax. I am only aloud to input the first set of values one time so I can put in title,name, price and count only once. I should be allowed to enter those values 8 times.

Here is my main 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
#include "hw.h"
#include <string>
#include <iostream>
using namespace std;
int bookinfo[8];
int main()
{
	bstore basicinfo;
		
	string basicinfotitle;
	string basicinfoauthor;
	float basicinfoprice;
	int basicinfocount;
	int j;
	for(j=0;j<8;j++){
	basicinfotitle=basicinfo.title();
	basicinfoauthor=basicinfo.author();
	basicinfoprice=basicinfo.price();
	basicinfocount=basicinfo.Count();
	}
	


}
you can enter it 8 times, but it gets overwritten 7 times, so only the last value is saved...
Topic archived. No new replies allowed.