problems with "if" 's, files, and do's. please help [Archive] - C Board

PDA

View Full Version : problems with "if" 's, files, and do's. please help


Unregistered
06-21-2002, 01:53 PM
this chunk of code displys NOTHING why is that?
char data[50];


do
{
fin>>data;
if(data == "{title")
{
fin>>data;
cout << data << endl;
}
if(data == "{zdescription")
{
do
{
if(i == 0)
{
fin>>data;
i=1;
}
cout << data << endl;
fin>>data;
} while(data != "}zdescription");
i = 1;
}
if(data == "{exits")
{
fin>>up;
fin>>down;
fin>>north;
fin>>south;
fin>>east;
fin>>west;
cout << "Exits[ ";
if(up != 0)
cout << "-up ";
if(down != 0)
cout << "-down ";
if(north != 0)
cout << "-north ";
if(south != 0)
cout << "-south ";
if(east != 0)
cout << "-east ";
if(west != 0)
cout << "-west ";
if((up == 0) && (down == 0) && (north == 0) && (south == 0) && (east == 0) && (west == 0))
cout << "None! ";
cout << "]" << endl;
}
} while(data != "}");



This is an infinate do and i do not know why, It also does not display ANYTHING... Here is the file that it reads:

}
{title
hole of incarnation
}title
{zdescription
you are in hole surrounded by lifeless bodys, waiting to be awakend...
}zdescription
{exits
3
0
0
0
0
0
}exits
{items
}items
{mobs
}mobs
{specials
}specials
}

What is wrong here???? (I am use microsoft visual C++)

TravisS
06-21-2002, 04:34 PM
Originally posted by Unregistered
this chunk of code displys NOTHING why is that?


char data[50];


do
{
fin>>data;
if(data == "{title")
{
fin>>data;
cout << data << endl;
}
if(data == "{zdescription")
{
do
{
if(i == 0)
{
fin>>data;
i=1;
}
cout << data << endl;
fin>>data;
} while(data != "}zdescription");
i = 1;
}
if(data == "{exits")
{
fin>>up;
fin>>down;
fin>>north;
fin>>south;
fin>>east;
fin>>west;
cout << "Exits[ ";
if(up != 0)
cout << "-up ";
if(down != 0)
cout << "-down ";
if(north != 0)
cout << "-north ";
if(south != 0)
cout << "-south ";
if(east != 0)
cout << "-east ";
if(west != 0)
cout << "-west ";
if((up == 0) && (down == 0) && (north == 0) && (south == 0) && (east == 0) && (west == 0))
cout << "None! ";
cout << "]" << endl;
}
} while(data != "}");



This is an infinate do and i do not know why, It also does not display ANYTHING... Here is the file that it reads:

}
{title
hole of incarnation
}title
{zdescription
you are in hole surrounded by lifeless bodys, waiting to be awakend...
}zdescription
{exits
3
0
0
0
0
0
}exits
{items
}items
{mobs
}mobs
{specials
}specials
}

What is wrong here???? (I am use microsoft visual C++)

OK, now we can get a better look at it

danielthomas3
06-23-2002, 12:54 AM
First off I'm new, so everything I say may be untrue. I'm just trying to help.

----------------------------------------


fin>>data;


try using cin>>data; instead. that's fin with a "c".

----------------------------------------


if(data == "{title")

try not using quotation marks, or a { symbol. like:


int title // make sure that you have initialized all variables.
if(data == title)


----------------------------------------

that should help, just use those two problem sovlers for ur code over and over, and hopefully it will help.

ihsir
06-23-2002, 01:29 AM
fin>>data


this will read a single char from the file. use fin.getline(data,size);
size -> means data[size]


if(data == "{title")


use if(strcmp(data,"{title")==0)
0 means matching/same.



fin>>data;
if(data == "{title")
{
fin>>data;
cout << data << endl;
}


what this piece does is : read 1 char from file, compare that single char to {title [which btw is not correct] and then if the comparision is correct read another single char and display it on screen [are you sure this is what you want]

i think your whole code which is posted here will have to be re-written to accomodate the changes.