Can A Char Be "<,>,="?


Recommended Posts

I was up making a mini proggie to test out what I learned, and I had this (After the beginning stuff):

char answer1;
cout <<"Is 4 (<,>,=) to 7?";
cin >> answer1

Can someone enter a symbol as a char?

Link to post
Share on other sites

we talked on AIM.. like I said, I don't think those can be stored in a char, and I don't think the computer would be able to recognize their meanings in relation to the cout text. Try putting each option represented by an int (eg 1. < 2. > etc...) and work from there.

Matt

Link to post
Share on other sites

it works as long as you case it correctly..

the nice part is you can chnage this so questions and value coem from a file....

#include <iostream>

using namespace std;

int main ()
{
       char Test;
       int a = 12;
       int b = 7;

       cout << "Answer is 12 > or < 7 (enter > or <): ";
       cin >> Test;

       switch ( Test ) {
               case '>':
                       if ( a > b ){
                       cout << "correct answer" << endl;}
                       else
                       cout << "wrong answer looser" << endl;
                       break;
               case '<':
                       if (a < b ){
                       cout << "correct answer" << endl;}
                       else
                       cout << "wrong answer looser" << endl;
                       break;
               }
       return 0;
}

Edited by iccaros
Link to post
Share on other sites

For future reference, the basic source character set, the character set available for use in source code, consists of the following characters:

ABDEFGHIJKLMNOPQRSTUVWXYZ

abcdefghijklmnopqrstuzwxyz

0123456789

!"#%&'()*+,-./:;<=>?[\]^_{|}~

and space, and control characters for horizontal (\t) and vertical (\v) tabs, form feed (\f), and newline (\n).

The basic execution character set, the character set that can be used at runtime e.g. for input, consists of all the source characters plus control characters for alert (\a), backspace (\B), carriage return (\r), and the null character (value 0).

Some implementations may support other characters, for example most support the entire ASCII character set and a great many support 8-bit character sets (e.g. various ISO 8859).

There's also wide-character sets (used with the type wchar_t) that are potentially (much) larger.

Edited by jcl
Link to post
Share on other sites

Cool! Thanks. Like Matt said, I will look into that too.

I was just making a proggie for Ch 5 of the book, to demonstrate looping...Now this stupid RHIDE program is telling me the a "break;" is not inside a loop -- which it is...(or I think it is...).

// a simple program. Used to be as a math "test". Multiple choice (3 for now,
// can be changed.... Also for what I learned in CH. 5

#include <cstdio>
#include <cstdlib>
#include <iostream.h>

int main ()
{

cout <<"Welcome to this program. This will ask you a few questions. Answer 1 for <, 2 for >, and 3 for =.\n";

cout <<"Question 1: \n";
int answer1;
cout <<"4 is <, >, = to 7??";
cin >> answer1;

//If answer right
if (answer1 == 1)

{

cout <<"\nThat answer is correct.";

}
{

cout <<"\nWrong. The correct answer is >.";
}

int answer2;
cout <<"\n5.0211 is <, >, = to 5.2011?";
cin >> answer2;

if (answer2 == 2)

{

cout <<"\nCorrect.";


}
{

cout <<"\nWrong. The correct answer is <.";

}

int answer3;
cout <<"\n5 over 8 is <, >, = to .635?";
cin >> answer3;

if (answer3 == 2)

{

cout <<"Correct";

}
{

cout <<"Incorrect. The correct answer is <.";

}


int exit;
cout <<"\nDo you want to quit? (0 for quit, 1 to start over)";
cin >> "exit";


if (exit == 0)

{

break;  //this is the break; where the compiler is giving me errors

}

{

system("PAUSE");
return 0;

}
}

Link to post
Share on other sites

if (exit == 0)

{

break; //this is the break; where the compiler is giving me errors

}

else

{

system("PAUSE");}

return 0;

Link to post
Share on other sites
I was just making a proggie for Ch 5 of the book, to demonstrate looping...Now this stupid RHIDE program is telling me the a "break;" is not inside a loop -- which it is...(or I think it is...).

There aren't any loops in that code.

Also, you should be using iostream instead of iostream.h. The latter is pre-ISO C++. And using 'exit' as a variable name when the exit() function is in scope is asking for trouble.

Edited by jcl
Link to post
Share on other sites

Ok, I changed the name, now it's still saying that break isn't in the code...

There aren't any loops in that code.

Why not...the "if" command was in the "Loop" section of the book...

I just put a bracket on the end of the whole thing, now its saying... "Virtual Memory Exausted."

Link to post
Share on other sites
Why not...the "if" command was in the "Loop" section of the book...

Conditionals don't create loops in the flow of control: the flow enters the conditional, possibly takes one of the branches, and then exits the conditional and continues on. Why they would be introduced as loops I cannot say.

I just put a bracket on the end of the whole thing, now its saying... "Virtual Memory Exausted."

Where at the end?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...