Hi all... I need to write a C++ program saying "Are you single or married? (s/m)?" It needs to accept the 's' or 'm' in it's capital or lowercase form, using an if-else statement...and if a different letter is entered, such as a 'q', the output must read "invalid input, please re-enter." I can't do it, please post some help, ASAP. THanks.
What about this? <b>void</b> main( ) { <b>char</b> var; cout << "Are you single or married (s/m)?" << endl; cin >> var; var = tolower(var); <b>while</b> (var != 'm' && var != 's') { cout << "Invalid input, please re-enter." << endl; cin >> var; var = tolower(var); } <b>if</b> (var == 'm') { cout << "You are married." << endl; } <b>else if</b> (var == 's') { cout << "You are single." << endl; } getch(); }
ummm, I don't think we should be doing each other's homework here... shouldn't this be used more for specific questions like how the hardware talks to the software and other computer-related stuff? I've programmed a tile based game engine in C++ so seriously, you gotta do your own work so it's 2nd nature by the time you do something big
As someone who's been programming since the mid-70s, I tend to agree. If you have a problem, the very least you should do is post the code (segment) for feedback. btw - interesting coding question: Given byte variables x and y, swap their contents without using a 3rd (temporary) variable. Please Register or Log in to view the hidden image!
Given byte variables x and y, swap their contents without using a 3rd (temporary) variable Couldn't you make a pointer to them and make anytime thay are accessed make them hit the pointer and go to the other one? It doesn't swap but it has the same effect!
#include <iostream.h> void swap(int *a, int *b) { int *x = &a, *y = &b; *a = y; *b = x; } void main(void) { int a = 1, b = 2; swab(&a, &b); cout << a << " " << b << endl; } /* darnit, it seems simple until I do it. Then it seems impossible. I used 4 variables in the function... I seriously think there's a way to do this using memory addresses but have yet to figure that out. I will keep my brain occupied with this one in the near future. */ I could be cheap and use void swap(int a, int b) { a = b; b = ::a; } // but I don't count that as being very effective. Unless maybe there's a dynamic way to use externs with the same name, heh. (with the same name makes it kinda undynamic). Also ::a can count as a 3rd variable.
Swapping vars... only for integers. A ^= B; B ^= A; A ^= B; or A = A ^ B; B = B ^ A; A = A ^ B; Example: A 1101 B 0011 A = A ^ B; A 1110 B 0011 B = B ^ A; A 1110 B 1101 A = A ^ B; A 0011 B 1101 -AntonK