I'm having trouble reading from my files properly.
the files being read from is in this format:
Q:3
D:4
N:1
and that is all in the file, it stands for quarters, dimes, nickels. yeah
and the function i have to read from that is this:
void initialize (ifstream& soda, ifstream& coin, SodaMachine* soda_machine)
{
string coin_line;
char coin_type;
char temp_char;
int num_coins;
do
{
//Read in line from file
coin >> coin_type;
//Check to make sure coin_type is a valid type
switch (coin_type)
{
case 'Q':
coin_type = 'q';
break;
case 'q':
coin >> temp_char;
if (temp_char==':')
{
coin >> num_coins;
if (num_coins >= 0)
{
soda_machine->quarters=num_coins;
}
else
{
//num_coins is less than 0 or soda failed
}
}
else
{
//Second character was not a colon
}
break;
case 'D':
coin_type = 'd';
break;
case 'd':
coin >> temp_char;
if (temp_char==':')
{
coin >> num_coins;
if (num_coins >= 0)
{
soda_machine->dimes=num_coins;
}
else
{
//num_coins is less than 0 or soda.cin failed
}
}
else
{
//Second character was not a colon
}
break;
case 'N':
coin_type = 'n';
break;
case 'n':
coin >> temp_char;
if (temp_char==':')
{
coin >> num_coins;
if (num_coins >= 0)
{
soda_machine->nickels=num_coins;
}
else
{
//num_coins is less than 0
}
}
else
{
//Second character was not a colon
}
break;
case '@': //should be default
//First letter in line is not Q, q, D, d, N, or n
break;
}
}
while (coin);
}
There is a soda_machine is a struct that has an array of dispensers, and the money stored inside the soda machine.
instead of getting numbers like 4 and 5 for the quantity of numbers
i get numbers like 2349183640.
can anyone suggest something?
thanks