what is EOF key in C++ - c++

I am using Windows 7 Ultimate. I am new to C++. Following is my exercise for switch statement.
void GradeBook::inputGrades()
{
int grade;
cout << "Enter Grade: " << endl;
while((grade=cin.get()) != EOF)
{
switch(grade)
{
case 'A':
case 'a':
aCount++;
break;
case 'B':
case 'b':
bCount++;
break;
case 'C':
case'c':
cCount++;
break;
case 'd':
case 'D':
dCount++;
break;
case 'F':
case 'f':
fCount++;
break;
case '\n':
case ' ':
case '\t':
break;
default:
cout << "Incorrect data. Re Enter" << endl;
break;
}
}
}
I run this inside netbeans, and I pressed all the combinations ctrl+c , ctrl+z, ctrl+d but it is not ending!! Why is that? Have I done something wrong? Please help!!

An EOF character is Ctrl+Z followed by a newline character on Windows platforms.
Presumably that will be the same for the console within Netbeans.

cin.get() is pretty low level. The code should use a higher-level interface. It's supposed to read a character at a time, so write it that way:
char grade;
while (cin >> grade)
The stream extractor will fail at end of file, and that will make the while loop terminate.

Related

Loop control inside void function keeps looping

void getDay() {
bool repeat;
do
{
cout << "Enter the day code (first 2 letters): ";
cin >> weekDay1;
cin >> weekDay2;
weekDay1 = toupper(weekDay1);
weekDay2 = toupper(weekDay2);
switch (weekDay1)
{
case 'M':
break;
case 'T':
break;
case 'W':
break;
case 'F':
break;
case 'S':
break;
default:
cout << "Invalid input. Please try again.\n";
repeat = true;
break;
}
switch (weekDay2)
{
case 'O':
break;
case 'U':
break;
case 'E':
break;
case 'H':
break;
case 'R':
break;
case 'A':
break;
default:
cout << "Invalid input. Please try again.\n";
repeat = true;
break;
}
}while (repeat == true);
return;
}
I need this function to run once, and loop if the input is not one of the accepted characters. I'm trying to prevent any bad input, but it loops infinitely if the input entered on the initial run is not accepted. It works fine if the input is good on the first run, but I keep getting run-time errors for not initializing bools and I need some help adjusting this control.
The condition in the while loop is always true because you never set it to false in its body. You can do something like this:
void getDay() {
// Initializing while declaring is a good practice.
bool repeat = false;
do {
.
.
repeat = false;
.
switch(...) {
...
}
} while (repeat);
}
Now, repeat = true is only called if one of the switch statements invokes default.

c++ default statement will be printed multiple times

void menu() {
char mode = ' ';
cout << "Gebe einen Modus an. 1 Addition, 2 Subtraktion, 3 Multiplikation, 4 Division: ";
cin >> mode;
switch (mode) {
case '1':
addition();
break;
case '2':
subtraktion();
break;
case '3':
multiplikation();
break;
case '4':
division();
break;
default:
cout << "Ungueltige Eingabe, versuch es nochmal\n";
menu();
break;
}
}
Hey! I have the problem, that If I input more than one character, which are not valid, the default statement will be executed just as often as the length of my input. But shouldnt It be like that:
If I input to a char more than one letter, everything after the letter will be cut off, since a char can only save one character. So why does it get executed multiple times? Could anyone explain this, detailed? Thanks in Advance!

I solved a thing, don't know the reason behind

I did a program that translates number into roman numbers, and I was about to show it to a friend of mine when the .exe file didn't work. It asked the user which number to put in, and after he put it the program closed. What I don't understand is why it did work after compiling on DevC++, but had a different behavior as a .exe
I found few solutions out there, and the one that worked for me was to add a:
int a;
cin>>a;
Before returning a cero. Now it works. I don't understand how the console doesn't execute the actions given without that. I'm leaving the code here.
#include <iostream>
using namespace std;
int main(){
int numero, unidades, decenas, centenas, millares;
int comprobante;
cout<<"Este programa traduce un numero de 4 cifras a numeros romanos.\nDigite un numero de
maximo cuatro cifras: ";
cin>>numero;
unidades= numero%10;
numero /= 10;
decenas= numero%10;
numero/=10;
centenas= numero%10;
numero/=10;
millares=numero;
comprobante=millares/10;
if(comprobante==0){
cout<<"Su numero traducido a numeros romanos es: ";
switch(millares){
case 1: cout<<"M"; break;
case 2: cout<<"MM"; break;
case 3: cout<<"MMM"; break;
}
switch(centenas){
case 1: cout<<"C"; break;
case 2: cout<<"CC"; break;
case 3: cout<<"CCC"; break;
case 4: cout<<"CD"; break;
case 5: cout<<"D"; break;
case 6: cout<<"DC"; break;
case 7: cout<<"DCC"; break;
case 8: cout<<"DCCC"; break;
case 9: cout<<"CM"; break;
}
switch(decenas){
case 1: cout<<"X"; break;
case 2: cout<<"XX"; break;
case 3: cout<<"XXX"; break;
case 4: cout<<"XL"; break;
case 5: cout<<"L"; break;
case 6: cout<<"LX"; break;
case 7: cout<<"LXX"; break;
case 8: cout<<"LXXX"; break;
case 9: cout<<"XC"; break;
}
switch(unidades){
case 1: cout<<"I"; break;
case 2: cout<<"II"; break;
case 3: cout<<"III"; break;
case 4: cout<<"IV"; break;
case 5: cout<<"V"; break;
case 6: cout<<"VI"; break;
case 7: cout<<"VII"; break;
case 8: cout<<"VIII"; break;
case 9: cout<<"IX"; break;
}
}
else{
cout<<"Te dije de 4 cifras. Por desgraciado me cierro.";
}
int a;
cin>>a;
return 0;
}
The program closes immediately in the terminal when the program reaches completion. When you cin>>a it is waiting for input before closing. See this.
Include
#include<conio.h>
And add getch(); at the end of your code. This will make your console to not close.
Your code was totally fine.
It is that when the main() reached return 0; that means it is done. And if you are running it as an .exe file, that means the program is done. And when a program is done, it would be closed.
There are couple ways to make it stay not closed. One that many have mentioned is to add a cin >> something; or getch(); with #include <conio.h> for windows at the end, so it would wait till you enter something.
Also you could delay the program before it reaches the return statement. You can check here How do you add a timed delay to a C++ program?
You could also wrap you entire program with a while loop, like:
while(true)
{
// Your programs here
if(someCondition) break;
};
where someCondition is a bool type, maybe char c; cin >> c; bool someCondition = (c == 'x'); So that the program only ends if someone input 'x', else it would run again.

Confusion Converting Hexadecimal to Binary in C++

#include <string>
#include <iostream>
using namespace std;
int main() {
string input, numBin = "";
cout << "Enter a hexadecimal number: ";
getline(cin, input);
for (int i = 0; i < input.length(); i++) {
switch (input[i]) {
case 0: numBin.append("0000"); break;
case 1: numBin.append("0001"); break;
case 2: numBin.append("0010"); break;
case 3: numBin.append("0011"); break;
case 4: numBin.append("0100"); break;
case 5: numBin.append("0101"); break;
case 6: numBin.append("0110"); break;
case 7: numBin.append("0111"); break;
case 8: numBin.append("1000"); break;
case 9: numBin.append("1001"); break;
case 'a': numBin.append("1010"); break;
case 'A': numBin.append("1010"); break;
case 'b': numBin.append("1011"); break;
case 'B': numBin.append("1011"); break;
case 'c': numBin.append("1100"); break;
case 'C': numBin.append("1100"); break;
case 'd': numBin.append("1101"); break;
case 'D': numBin.append("1101"); break;
case 'e': numBin.append("1110"); break;
case 'E': numBin.append("1110"); break;
case 'f': numBin.append("1111"); break;
case 'F': numBin.append("1111"); break;
default: break;
}
}
cout << "Your number in binary is " << numBin << ".";
}
This program is supposed to change a hexadecimal input ('input') into a binary result ('numBin'). I don't have much experience using switch statements and do not fully understand the "default" case, so any clarification about that or if I am using it incorrectly would be helpful!
The error I'm getting is on the for loop, and it thorws: comparison between signed and unsigned integer expressions [-Wsign-compare]
In the line:
for (int i = 0; i < input.length(); i++) ...
input.length() returns a size_t, which is a unsigned type.
(see http://www.cplusplus.com/reference/string/string/length/)
Comparing signed and unsigned values is not safe, which is why the compiler warns you, read more about it in this post among many others:
A warning - comparison between signed and unsigned integer expressions
To fix it, simply change to
unsigned int i = 0
The default switch case will be executed when none of the other cases match. You should put some code there that handles incorrect input for example.
case '0':
case '1':
...
Use all characters..not number and characters.
And one ore thing..for(i=0;i<(int) input.length();i++)

how to properly create an encryption in c++?

I have finished my program about encryption. The problem is, I need a shortcut. The program I have coded is too long and not very suitable. I'm just new to c++ programming. Can someone help me out? thanks! :) Here's my program:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
c:char crypt[25], cons;
int ctr;
cout<<"Input your 26-character cipherstring below.\n\n";
cout<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";cin>>crypt;
e:char string[65535];
cout<<"\nInput your string (input your spaces as any non-alphabet character).\n";cin>>string;
cout<<"\n\nEncrypted string: "; //std::string
for(ctr=0;ctr<=strlen(string);ctr++)
{
switch(string[ctr])
{
case('a'): cout<<crypt[0]; break;
case('b'): cout<<crypt[1]; break;
case('c'): cout<<crypt[2]; break;
case('d'): cout<<crypt[3]; break;
case('e'): cout<<crypt[4]; break;
case('f'): cout<<crypt[5]; break;
case('g'): cout<<crypt[6]; break;
case('h'): cout<<crypt[7]; break;
case('i'): cout<<crypt[8]; break;
case('j'): cout<<crypt[9]; break;
case('k'): cout<<crypt[10]; break;
case('l'): cout<<crypt[11]; break;
case('m'): cout<<crypt[12]; break;
case('n'): cout<<crypt[13]; break;
case('o'): cout<<crypt[14]; break;
case('p'): cout<<crypt[15]; break;
case('q'): cout<<crypt[16]; break;
case('r'): cout<<crypt[17]; break;
case('s'): cout<<crypt[18]; break;
case('t'): cout<<crypt[19]; break;
case('u'): cout<<crypt[20]; break;
case('v'): cout<<crypt[21]; break;
case('w'): cout<<crypt[22]; break;
case('x'): cout<<crypt[23]; break;
case('y'): cout<<crypt[24]; break;
case('z'): cout<<crypt[25]; break;
case('A'): cout<<crypt[0]; break;
case('B'): cout<<crypt[1]; break;
case('C'): cout<<crypt[2]; break;
case('D'): cout<<crypt[3]; break;
case('E'): cout<<crypt[4]; break;
case('F'): cout<<crypt[5]; break;
case('G'): cout<<crypt[6]; break;
case('H'): cout<<crypt[7]; break;
case('I'): cout<<crypt[8]; break;
case('J'): cout<<crypt[9]; break;
case('K'): cout<<crypt[10]; break;
case('L'): cout<<crypt[11]; break;
case('M'): cout<<crypt[12]; break;
case('N'): cout<<crypt[13]; break;
case('O'): cout<<crypt[14]; break;
case('P'): cout<<crypt[15]; break;
case('Q'): cout<<crypt[16]; break;
case('R'): cout<<crypt[17]; break;
case('S'): cout<<crypt[18]; break;
case('T'): cout<<crypt[19]; break;
case('U'): cout<<crypt[20]; break;
case('V'): cout<<crypt[21]; break;
case('W'): cout<<crypt[22]; break;
case('X'): cout<<crypt[23]; break;
case('Y'): cout<<crypt[24]; break;
case('Z'): cout<<crypt[25]; break;
default: cout<<" "; break;
}
}
cout<<"\n\n";
/*cout<<"Input 'c' to re-input your cipherstring.\n 'e' to reuse your cipherstring.\n 'q' to quit. ";
comm:cout<<"\nCommand: "; cin>>cons;
switch (cons)
{
case('c'): cout<<endl; goto c; break;
case('C'): goto c; break;
case('e'): goto e; break;
case('E'): goto e; break;
case('q'): break;
case('Q'): break;
default: cout<<"Invalid command. Please refer to the command list above.\n";goto comm;
}*/
system("PAUSE"); return 0;
}
You can replace that huge switch statement with this:
if (isalpha(string[ctr]))
{
int index = toupper(string[ctr]) - 'A';
cout << crypt[index];
}
else
cout << " ";
Also, do not call strlen for every iteration of the for loop, just do it once:
for(int ctr=0, len=strlen(string); ctr<len; ctr++)
{
....
}
A few other things of note:
crypt is too small, it should have size 27
If you use getline instead of operator>>, you can have the user enter spaces properly
Your code is prone to buffer overflows. Use std::string and you can pretty much forget about those.
Note, that as Kerrek says, this assumes that in the character set you are using, the uppercase letters A-Z are in sequence. But I think that the cases where this is not true are so exceedingly rare, that you can safely ignore them.