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
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.
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)
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!!
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.
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 ;)
@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.