Ich mache ein Labor für meine C++ - Klasse in der Schule. Das Programm liest Binärzahlen aus einer Datei ein, berechnet dann deren Dezimaläquivalent und gibt sie an die Konsole aus, muss aber die gesamte Datei zeichenweise lesen. Das Programm soll führende Leerzeichen und Nullen ignorieren. Wenn das Programm eine ungültige Ziffer erreicht, möchte ich es an den Anfang der Zeile ein Zeichen zurückzugehen, dann Gedankenstrich und wie so aus der Meldung „Bad Ziffer auf Eingang“ drucken:C++: Zeichen, die bereits in der Konsole ausgegeben wurden, können vor der Ausgabe nicht gelöscht werden.
Alle von der „Bad Zifferneingabe“mit dem Rest der Zahlen in den Binärzahl Spalt ausgerichtet bleiben, aber wie Sie sehen können ...
Hier ist das gesamte Programm:
Hierint main(void)
{
//Declarations
ifstream infile;
char x = 0;
int y = 0, i = 0, count = 0, q = 0, l = 0;
//Prints out header to console
cout << left << setw(20) << "Binary Number" << right << setw(15) << "Decimal Equivalent" << endl;
//Opens data file
infile.open("INLABVII.dat");
//Will loop through file until it reaches <eof>
while(!infile.eof())
{
//Gets character from file
infile.get(x);
//Digit is a valid binary digit
if(x == '1' || x == '0')
{
//First binary digit read in
if(y == 0)
{
//Backspace any leading spaces or zeros
for(i = 0; i < count; i++)
{
cout << '\b';
}
//Add 6 spaces to indent line
for(q = 0; q < 6; q++)
{
cout << " ";
count++;
}
if (x == '1') //First digit was a 1
{
y = y*2+1;
cout << "1";
count++;
l++;
}else //First digit was a 0
{
y = y*2;
cout << "0";
count++;
l++;
}
}else if(y != 0)
{
if (x == '1') //Digit was a 1
{
y = y*2+1;
cout << "1";
count++;
l++;
}else //Digit was a 0
{
y = y*2;
cout << "0";
count++;
l++;
}
}
}else if(x == ' ') //Read in a space
{
if(y == 0) //Was a leading space
{
count++;
}else if(y != 0) //Now testing for if it is a space at the end of the line
{
//Gets character from file
infile.get(x);
if(x == '\n') //Space was at the end of the line
{
cout << right << setw(15) << y << endl;
y = 0;
count = 0;
}else //Space was either in the middle of the number, or there was more than one at the end of the line
{
//Backspace to beginning of current line on console
for(i = 0; i < count; i++)
{
cout << '\b';
}
//Add 6 spaces to indent line
for(q = 0; q < 6; q++)
{
cout << " ";
count++;
}
cout << left << setw(25) << "Bad digit on input" << endl;
y = 0;
count = 0;
//Skip to next line if infile has invalid digit on current line
infile.ignore(100, '\n'); //Skip bad input
}
}
}else if (x == '\n') //End of line
{
cout << right << setw(15) << y << endl;
y = 0;
count = 0;
}else if(x != '1' && x != '0' && x != ' ' && x != '\n') //Invalid digit was read in
{
//Backspace to beginning of current line on console
for(i = 0; i < count; i++)
{
cout << '\b';
}
//Print out 6 spaces to indent line
for(q = 0; q < 6; q++)
{
cout << " ";
count++;
}
cout << left << setw(25)<< "Bad digit on input" << endl;
y = 0;
count = 0;
//Should skip to next line if infile has invalid digit on current line
infile.ignore(100, '\n'); //Skip bad input
}
}
return 0;
}
ist, was ich in meiner Input-Datei (I ersetzt _ für "" so würden sie sichtbar sein):
Von dem, was ich weiß, Looping cout << '\b';
eine bestimmte Anzahl von Zeiten sollten Backspace die Anzahl der Zeichen, aber aus irgendeinem Grund ist meine nicht.
Bearbeiten: Hinzugefügt cout << '\r';
. Hier ist meine neue Ausgabe. Jetzt habe ich zufällig leere Zeilen und die Eingabe mit führenden Nullen wird immer noch nicht korrekt ausgedruckt.
Edit: die führende Null Problem wurde behoben. Das Hinzufügen von cout << '\r';
hat gerade "Bad digit on input" in die nächste Zeile verschoben, wobei die bereits gedruckten Zeichen in der Zeile darüber stehen bleiben.
Offish Thema: 'while (! Infile.eof())' wird nicht so gut für Sie arbeiten. Mehr hier: http://stackoverflow.com/questions/5605125/why-is-iostreamof-inside-a-loop-condition-conspired-wrong – user4581301
Es ist in der Regel einfacher, Sachen an erster Stelle zu schreiben, als die Konsole aufzuräumen danach. Halte deine Ausgabe, bis du weißt, dass du sie schreiben willst, und schreibe sie dann. – user4581301
'while (infile.get (x))' wurde in der Klasse nicht behandelt? Ungerade. Ich sehe sowohl "while" als auch "infile.get (x)" in Ihrem Code. – user4581301