C++ Workshop - Looping & Switch Statements (Ch. 7)

Started by
48 comments, last by Dbproguy 16 years ago
There is nothing special about 'Q' that would make cin fail. IIRC, cin will fail on EOF.

Now, streams do have a fail state. The only way to check and see if 'Q' causes a failure when streaming into an int is to test it. In my experience, it just ignores invalid characters.
Advertisement
I would like to ask wether each color is connected with a particular number.

If i right this for example

SetConsoleTextAttribute(hConsole,12);

text turns into bright red.
If i change the number i get a different color.
Quote:Original post by Deyja
There is nothing special about 'Q' that would make cin fail. IIRC, cin will fail on EOF.


Interesting... well, it's failing and dropping into the appropriate block. Perhaps because it's trying to shove 'Q' into userInput variable which is declared as an int?

But with respect to my question, I was just looking for a more appropriate or cleaner way to handle this since I wasn't too keen on this methodology anyway...

Perhaps my brain is getting hung up on the fact that we're dealing with two types of data here: namely several integer options and one character option.

Is there a better, more versatile way to handle it? Perhaps by capturing the input as a string (or character), checking to see if it's an int or a char, then handling it appropriately? Or am I overanalyzing the situation?

Thanks!
evan k. stone | recombinant---------------------------------------aspiring game + audio programmersf bay area, ca, usa
Hi everyone i have just finished chapter 7 and i got stuck on the for loop that completes the fibonacci problem with iteration. I was getting confused with the actual initialzation part of the for loop.

for (position -= 3; position != 0; position--)
{
minusTwo = minusOne;
MinusOne = answer;
answer = minusOne + minusTwo;
}

return answer;

What was confusing me was that you passed in the value for position and then the position was minused the value of 3 and then it was tested if it wasnt equal to 0 the body of the loop ran and assigned a value into answer, and then the position was decrement by 1. Now i thought if you put a value for position to be 5, I thought position would be minus three then minused one and then at the start of the for loop poitions value of 1 would be minused 3 again making the value wrap around to the largest value that an unsigned integer could handle minus 2.

So i put a statement in it to test the value that position holds and print it to the screen.

for (position -= 3; position != 0; position--)
{
cout << position << endl;
minusTwo = minusOne;
MinusOne = answer;
answer = minusOne + minusTwo;
}

Now when i put the value 5 in for the value of position i can see that the initialization only happens once. brcause the uotput for position is 2 and then 1 then of course 0 and the test of the for statement is false and the body of the for statement is not executed and then it return the value of answer.

I have two questions, once the initialization in a for statement is executed once, has that part of the for statement finished? If there are any more iterations to be carried out in the for loop then it is only the test and action that is performed and the initialization part is left out?

thank you in advance for any help
Its ok i think i have found the answer on page 189 in the grey box bit on the page, under "the for statement" syntax for the for statement must of missed it the first time through lol.
Just posting that I have thus finished up to this chapter of the workshop and am finding all the information in each thread very enlightening. I wish I would have started when the workshop began but only recently happened upon it. Hopefully I can catch up to the rest of the class =)

Here's to my first project I will be starting in the coming days!
I keep trying to install the Microsoft Platform SDK and get the following error:



Anyone know what I need to do to fix it? Do I need to install all of the files separately?
Download the complete package directly, rather than using the network installer. You might have to download a lot more than you would otherwise, for stuff you didn't want to install anyway, but the install itself will be much faster, and unaffected by broken links.
Hello ladies and gents,


I just finished the "C++ Workshop - Week 6 (Ch. 7) - Quizzes and Exercises", but, didn't find the correct solution for the Color Menu Exercise in that I didn't find a way to use the enum.

The trouble I had was that I tried to use casting, dynamic_cast, but didn't seem to find the correct solution for it. Either I couldn't cast a char because it worked for like '1' or '5' and 'Q', but didn't work for '10' OR '11'. So tried to use std::string then, with this, I couldn't get the string casted into an int and not only that, but, even when I tried to use an int, I still had to cast it to a COLOR value of my enum.

Could someone show me how you guys solved this problem.

Thank you.



JOBe,

People made this out to be far more complicated than it is. There are a few ways to do it, some easier, some more difficult. The easiest way is as follows:

1. Query the user for a string
2. Use cin or getline to obtain the string
3. Check to see if the first character of the string is a 'q' or 'Q' (check to make sure the string is exactly 1 character long if you like)
4. If so, quit. If not, convert the value in the string to an integer
5. Pass the integer to a switch statement, which uses the enum as the cases.

See, not so difficult. What trips most people up is part 4. There is an easy way to convert a string to an integer. It's a function called atoi(), which takes a string, and returns the matching integer if possible. If the string contains the value '12' it will return the number 12.

Alternatively, you can simply evaluate the string and convert it yourself. If the string is only 1 character long, then simply evaluate the character, and return the matching integer. If the string is more than 1 character long, for each place digit, evaluate the number, and then multiply the output accordingly. For example...

int eval('1') would find that the character matches the character '1', and so would return 1.

However, if the above '1' is in the tens digit, then you multiply the value by 10. If it's in the hundreds digit, you multiply it by 100. In this way you can iteratively advance through place digits and sum them to create a final value.

At any rate...once you've determined the integer value, you simply pass it to the switch statement

switch(myInteger){    case DARK_BLUE:        ...        break;    case DARK_GREEN:        ...        break;}


Depending on which case the user lands on, perform a different function. This was more for fun, however, because if I recall, the numbers I gave you for the color codes 1-15 were the actual values required by SetConsoleTextAttribute. So it was possible, once the value was converted to an integer, to pass it directly to that function.

Hope this helps.

Cheers!


Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement