//flex table opened by JP

Click to See Complete Forum and Search --> : C++ help


nothing
08-01-2004, 02:28 AM
int keep_looping = 0;
std::ifstream from_file("file.txt");

while(keep_looping == 0){

if(from_file.is_open()){
while(std::getline(from_file, line))

std::cout << "Last line read: " << line <<'\n';

parenthesis = line.find_first_of('(');

today = line.substr(0, parenthesis);

std::cout << "Today is " << today << '\n';
std::cout << "weekdays[weekday] is " << weekdays[weekday] << '\n';

if(today == weekdays[weekday]){
std::cout << "\nYou've already done this today...\n";
keep_looping = 1;
}
}


Can somebody tell me why the first if statement is not executing when file.txt exists and is opened? Thanks.

Tech^salvager
08-01-2004, 10:34 AM
Just a guess
if(from_file.is_open()){
while(std::getline(from_file, line))

if(from_file.is_open()}{
while(std::getline(from_file, line))

nothing
08-01-2004, 10:46 AM
That's not it Tech^salvager. Thanks for trying though. When you'll write only one statement inside a while loop, if, for, etc, there is not need for curly brackets.

while(true)
std::cout << "Hi there\n";



while(true){
std::cout << "Hi there\n";
std::cout << "How are you this morning?\n";
}


:t

CompGeek01
08-01-2004, 11:27 AM
Nothing, when I copied and tried the code I couldn't get the if statement NOT to execute. Even if I closed the file right before it.

Weird.

nothing
08-01-2004, 11:48 AM
Can you suggest me an alternative for

if(from_file.is_open())


?

Thanks :t

nothing
08-01-2004, 01:03 PM
Ok, I got it working somehow lol, but now I am having another weird problem.

while(keep_looping == 0){

if(from_file){
while(std::getline(from_file, line));
std::cout << "Line: " << line << '\n';

std::cout << "Last line read: " << line <<'\n';

parenthesis = line.find_first_of('(');

today = line.substr(0, parenthesis);

std::cout << "Today is " << today << '\n';
std::cout << "weekdays[weekday] is " << weekdays[weekday] << '\n';

if(today == weekdays[weekday]){
std::cout << "\nYou've already done this today...\n";
keep_looping = 1;
}
}

do{
system("cls");
std::cout << date.str() << '\n';
std::cout << "How many hours did you work today? ";
std::getline(std::cin, hours_worked);
}while(!validate_hours_worked(hours_worked));
}


The first if statement executes but for some reason that do-while statement will also execute. What am I doing wrong?

CompGeek01
08-01-2004, 05:05 PM
Well, do while will naturally execute once, but I'm sure you know that.

Have you tried stepping through with the debugger and watching variables?

Tech^salvager
08-01-2004, 05:36 PM
Thanks for the info. Just goes to show how new I am to thisThat's not it Tech^salvager. Thanks for trying though. When you'll write only one statement inside a while loop, if, for, etc, there is not need for curly brackets. Thanks for the info. Just goes to show how new I am to this.