I'm having a problem with cin.get().
I'm prohibiting the user, that he can only enter 5 keys.
and each key does something, like a MENU.
Press 1, if you want this, etc.

char choice[5];
    cout<<"ENTER YOUR CHOICE"<<endl;
    cin.get( choice, 5);
    
    switch (choice)

Okay, when I run this, the compiler gives me an error on switch (choice).

so I did,

switch (choice[0], choice[1], choice[2], choice[3], choice[4])

and that didn't give me a compiler error, but would this be correct?
Because when I run it. I can still key more than 5 letters.

You can only switch on integer values. This restriction makes more sense when you think of a switch statement as a table lookup where each case is an index into the table.

>switch (choice[0], choice[1], choice[2], choice[3], choice[4])
>and that didn't give me a compiler error, but would this be correct?
It doesn't give you an error because char is an integer type, but it's not going to do what you want (I suspect). Generally people who aren't familiar with the comma operator are surprised with the result.

Now for the problem at hand:

>I'm prohibiting the user, that he can only enter 5 keys.
You can't really restrict what the user enters, but you can handle invalid input gracefully:

#include <iostream>
#include <string>

bool is_valid ( const std::string& s )
{
    return s == "a" || s == "b" || s == "c";
}

int main()
{
    for ( ; ; ) {
        std::string choice;

        std::cout<<"Enter a choice: ";

        if ( getline ( std::cin, choice ) &&
             is_valid ( choice ) )
        {
            std::cout<<"Woo-hoo!\n";
        }
        else if ( std::cin.eof() )
            break;
        else if ( !std::cin )
            std::cout<<"Stream error\n";
        else
            std::cout<<"Invalid choice\n";
    }
}
commented: You're the expert in that stuff :) +1

In addition to Narue's code:
You could make a counter which only increments if the userinput was valid. When the counter hits 5 > break; That way you'll have 5 correct inputs and nothing more!

^
But isn't that the definition of cin.get()?

depending on the number inside the parameter field, the input will be that amount?

u should have to use cin instead of cin.get because cin.get mostly used in case of string
use cin as below


char choice[5];
cout<<"ENTER YOUR CHOICE"<<endl;
choice=getche;
switch(choice)
case '1':
sum=a+B;
break;
case '2':
multi=a*b;
//up to 5 choices and then default choice

u should have to use cin instead of cin.get because cin.get mostly used in case of string
use cin as below


char choice[5];
cout<<"ENTER YOUR CHOICE"<<endl;
choice=getche;
switch(choice)
case '1':
sum=a+B;
break;
case '2':
multi=a*b;
//up to 5 choices and then default choice

I'm at a loss. You say "u should have to use cin" and post code that doesn't use cin . What are you trying to do, confuse people? :icon_wink:

Yeah I'm kind of confused at what that guy posted.


But,

#include <iostream>
#include <string>

bool is_valid ( const std::string& s )
{
    return s == "a" || s == "b" || s == "c";
}

int main()
{
    for ( ; ; ) {
        std::string choice;

        std::cout<<"Enter a choice: ";

        if ( getline ( std::cin, choice ) &&
             is_valid ( choice ) )
        {
            std::cout<<"Woo-hoo!\n";
        }
        else if ( std::cin.eof() )
            break;
        else if ( !std::cin )
            std::cout<<"Stream error\n";
        else
            std::cout<<"Invalid choice\n";
    }
}

What is this std:: ?
I never seen that before.

>What is this std:: ?
>I never seen that before.
std is a namespace, and the whole construct is a namespace qualification that says, for example: "cout is a name in the std namespace" as opposed to "cout is a name...somewhere". The idea behind namespaces is that if you come up with your own cout, you can differentiate between them:

#include <iostream>

int cout = 4321;

int main()
{
    int cout = 12345;

    std::cout<< cout << ::cout <<'\n';
}

In this example ::cout specifies the cout in the global namespace, cout specifies the cout in the local scope of main, and std::cout specifies the standard cout from the iostream library.

You might have seen code like this, which is simply a different way of qualifying names in a namespace:

#include <iostream>

using namespace std;

int main()
{
    cout<<"Hello, world!\n";
}

If you haven't, you really should update your C++ knowledge because namespaces have been a part of the language since the early 90s. Many modern compilers will refuse to compile pre-standard code these days.

^
I have seen the latter. I was taught the latter.
Hmm, I thought of another way.
Would this do what I want?

char letter; 
   cout<<"Key in some letters: ";
   
   for (i=0; i<5; i++)
   {
   cin>> letter; 
   }

   switch (letter)
case '1';

// creates link list
case'2';
// couts some line.

etc.

No. Your loop reads each letter, overwriting the previous character. When the loop ends all you have is the 5th character entered

I knew it was too good to be true..
Damnit..

hmm would this suffice?

#include <iostream>
#include <string>

using namespace std;

bool is_valid ( const string& s )
{
    return s == "a" || s == "b" || s == "c";
}

int main()
{
    for ( int i=-0; i<5; i++)
    {
        string choice;
        cout<<"Enter a choice: ";

       if(is_valid(choice))
     {
       blah blah blah? 

     }
    }
}

In addition to Narue's code:
You could make a counter which only increments if the userinput was valid. When the counter hits 5 > break; That way you'll have 5 correct inputs and nothing more!

I don't think that'll work.
Anyways, I think cin.get() is buggy.

Because no matter how I used it.
I can always type more than what I want in a line.

Umm, unless I'm reading the question wrong.

It says don't let your list scroll off a 40 line screen.

does that mean.

1
..
.
.40

or

1 40 ?

>Anyways, I think cin.get() is buggy.
>I can always type more than what I want in a line.
It's buggy because you want it to do something it wasn't designed to do and clearly isn't documented to do? That makes sense.

I get the impression that you can't tell the difference between your running program and the console window. The console window acts as a shell, it takes input and passes that input to your program. As such, you have no control over how much the user types. Zero, zilch, nada. The only way to obtain control is to remove the shell from the picture and simulate its behavior in your code.

So were you not paying attention when I said:

You can't really restrict what the user enters, but you can handle invalid input gracefully

Or were you hoping I didn't mean what I said?

^
I was kind of hoping that you were joking ^^

So part of the program limitations is
" don't let your list scroll off a 40 line screen"

does that mean,
40 enter lines?

1
2
3
4

etc
...
?

>does that mean, 40 enter lines?
I haven't a clue what it means as you haven't done anything but paraphrase that one tiny part of your requirements. :icon_rolleyes:

Before that it says what the selections in the menu do.
Like 1 does this
2 does this.

Then it says, Be sure the contents of the list don't scroll off of a 40 line screen.

So I'm not really paraphrasing ><

>Be sure the contents of the list don't scroll off of a 40 line screen.
What list? You may not be paraphrasing, but you're being too vague. What exactly does this program do? Give us all of the requirements as written so we understand what you're trying to accomplish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.