Can't get loop working

i have a text file with 30 records in it with 4 people listed. A person will be in the text file many times with a different date beside the name each time

i.e. 21 4 2011 Joe
22 4 2001 Jerry.. and so on for 30 records

I want to be able to enter a name and display all the dates that a certain person was in the file i.e if i enter Joe all the dates that Joe is in the file will come up

heres the code i wrote but all it does is display one date when i enter a name and the name still doesn't match the date (example if i entered Jerry first then 21 4 2011 will display even though that's Joes date)

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < 30; i++)
	{
	string name;
	cout <<"Enter a name : " << endl;
	cin >> name;
	if (name == person[i].name)
	{		
	       infile >> date[i].day >> date[i].month >> date[i].year;
	}
				

cout << date[i].day << "/" << date[i].month << "/" << date[i].year << endl;
					}


If anyone can tell me where i'm going wrong here i'd really appreciate it
thanks
Last edited on
Eeeeeerm, just glancing at that code it looks wrong - not the syntax but the logic.
If you look again at this section:
1
2
3
string name;
cout <<"Enter a name : " << endl;
cin >> name;

Why is that inside the for loop? Shouldn't it be outside? I.e. you get a name to search for, then loop through the 30 records looking for a match.
Last edited on
ok I did that but now it's displaying all 30 dates instead of the dates next to the name I entered
Last edited on
Hi
What's displaying what?

This line:
cout << date[i].day << "/" << date[i].month << "/" << date[i].year << endl;
is outside of the if statement, so it's going to execute every iteration of the loop.

Whereas this:
infile >> date[i].day >> date[i].month >> date[i].year;
should only execute if names match.

Regards, keineahnung
thanks it worked

i have a similar problem with another loop if you could help with that

1
2
3
4
5
6
7
8
for (int i = 0; i < 30; i++)
	{
	      if(report[i].rainfall >=4)
	      {
	        infile >> date[i].day >> date[i].month >> date[i].year;
	        cout << "wet days are : " << date[i].day << "/" << date[i].month << "/" << date[i].year;
	      }
               }	    


for this program i have a text file with a list of 30 numbers and if any of the numbers are greater than or equal to 4 i want to display the dates next to it

the problem is like the last one. its displaying all the dates instead of the just the ones where the number is >=4
You may have a problem here:
if (name == person[i].name)
You aren't comparing the strings, but the pointers!
Try using
if(strcmp(name,person[i].name) == 0)
(strcmp returns 0 if the strings are equal, it's not a mistake)
Last edited on
ok thanks would you be able to help on a similar problem i have posted just above your reply?

thanks
Hi
Try inserting this:

1
2
3
4
5
6
cout << i << ": " << report[i].rainfall << "\n";
if(report[i].rainfall >=4)
{
    infile >> date[i].day >> date[i].month >> date[i].year;
    cout << "wet days are : " << date[i].day << "/" << date[i].month << "/" << date[i].year;
}


Just to make sure that the values you're comparing are what you think they are. Maybe they are >= 4 every time?! (even though they shouldn't be).
Last edited on
no its still showing all the dates instead of just the ones that are >=4
no its still showing all the dates instead of just the ones that are >=4

Hi, yes it will do, that wasn't a fix. It was just so you could maybe see what was going wrong. You should have seen some output like:
0: 5
1: 12
2: 3
3: 8

etc.
Can you post that up?
Try using
if(strcmp(name,person[i].name) == 0)
(strcmp returns 0 if the strings are equal, it's not a mistake)


He is using C++ strings. This is necessary only if he is using an array of chars. The string class has an overloaded operator == to check the values and not their addresses!!
i don't really want the output to look like that

I would prefer if it just said wet are days are:
then list all dates under

I have a piece of code in the prgroam thats nearly identical to this and it works fine which is why i don't understand why its not working
keineahnung's code is there to help us debug your problem. We can't help you if we don't know what's going wrong. The output will help us determine that.
i don't really want the output to look like that

Sorry you've misunderstood me. I meant: can you post that output up on this forum so we can see what values are being pushed into your if statement? Without knowing that it's impossible to comment on what might be going wrong.
Regards, keineahnung
*Edit*
Sorry, Gaminic, didn't see your post first ;)
Last edited on
ok heres what the output looks like

wet days are:
1/12/2009
2/12/2009
3/12/2009
4/12/2009
5/12/2009
6/12/2009
7/12/2009
8/12/2009
9/12/2009
10/12/2009
.
. and so on til 30
.
30/12/2009

so its displaying all 30 dates instead of just the dates where rainfall >=4

and with keineahnungs code it looks like this
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
57
wet days are:
0: 12.5
1/12/2009
1: 5.8
2/12/2009
2: 6.3
3/12/2009
3: 4.7
4/12/2009
4: 7.5
5/12/2009
5: 8.4
6/12/2009
6: 10.5
7/12/2009
7: 5.3
8/12/2009
8: 9.3
9/12/2009
9: 10.3
10/12/2009
10: 5.7
11/12/2009
11: 9.3
12/12/2009
12: 5.2
13/12/2009
13: 4.7
14/12/2009
14: 3.1
15: 5.9
16/12/2009
16: 6.1
17/12/2009
17: 2.9
18: 5.1
19/12/2009
19: 12.7
20/12/2009
20: 10.8
21/12/2009
21: 4.8
22/12/2009
22: 3.9
23: 7.8
24/12/2009
24: 4.1
25/12/2009
25: 10.8
26/12/2009
26: 6.9
27/12/2009
27: 10.4
28/12/2009
28: 4.7
29/12/2009
29: 2.5
Last edited on
I have a similar question I posted last night with no reply. If someone could take a peak at it... http://www.cplusplus.com/forum/beginner/51846/
@kw1991
so its displaying all 30 dates instead of just the dates where rainfall >=4

No it isn't. Look at the output again. For iteration 14, for example, rainfall is 3.1: the date doesn't get displayed.
Your code's working exactly the way it should.
your right thanks. i wasn't looking at the output right.

@ Nisheets: My fault. Thank you!
Topic archived. No new replies allowed.