English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

so i have a program that reads in a file and decodes the file then spits out the message. the symbol @ followed by an int will declare what amount i need to shift the alphabet by. i have all of that working fine. my problem is if there isn't an int following the symbol, the program displays an error i made. i need it to discard the rest of the message until it reaches the # symbol. when it reaches that, it will start behaving like normal again. i would appreciate any help.

2007-10-26 10:56:52 · 2 answers · asked by nadsoil05 1 in Computers & Internet Programming & Design

here is the code if have written -
while (inFile)
{
inFile.get (ch); //looks through the file character by character
if (ch == shiftdown)
{
inFile >> keytemp;
if (inFile.fail()) //no int following special symbol
{
inFile.clear();
cout << "*** Error Code 2 -- No shift value as expected ***";
break;
}
key = 0 - keytemp;
if (key < -26)
{
key = key % 26;
}
}
so what the code needs to do is if there is no number after the symbol to shiftdown, it needs to print that error message and then finish the line until the # sign.all of those characters just need to be thrown away and not displayed. once it gets there, it needs to start reading the new line again and apply the shift

2007-10-26 13:24:18 · update #1

2 answers

if @n occurs on a line by itself, where n is a number or invalid character.
Read the line in.
Call atoi on the string after @.
if atoi returns 0, assume invalid number entered then read characters until the character '#' is encountered.

do {
infile.getline(buffer, 80);
if (*buffer == '@') // if first character = '@'
{
shift = atoi(buffer + 1); // process characters after '@'
if (!shift && errno == ERANGE) // atoi sets errno on invalid value
{
while (!infile.eof() && infile.get() != '#');
continue; // processing file.
}
}
} while (!infile.eof());

2007-10-26 12:44:39 · answer #1 · answered by Anonymous · 0 0

you need to determine if there character after the @ is a digit.
if it is not a digit you have an error, and should read characters until you reach an # or an end of stream.

where exactly is the problem ?
is it the transformation from characters to int ?

2007-10-26 18:08:12 · answer #2 · answered by gjmb1960 7 · 0 0

fedest.com, questions and answers