

InterviewSolution
1. |
Solve : C++ coding? |
Answer» I wrote this code and I am unable to compile and run it. Everytime I try and compile it, I'm always getting a lot of errors and I can't seem to figure out what is really wrong. I am using Microsoft Visual Studio C++ 2010 as my compiler. Also, I am trying to substitute out the logical equality operator "==" with "<=" and also ">=" just to see what HAPPENS. Here is the code... char sex;char sex[]; Quote cout<<Susan.number_of_children<<ENDL…cout< Quote };Thinking you need to define something as an object between the } and ; such as in the code on this linked page: http://msdn.microsoft.com/en-us/library/64973255(v=vs.80).aspx The compiler itself gives you a lot of useful information to help point out syntax errors. If you click on the line in the error log it will highlight the line where the issue is, however sometimes the compiler cant interpret what your intentions were and so you get an error that doesnt point to a single line which can get frustrating especially if your code all looks good and its a difference between one compilers format and another. This can happen if you are missing an #include or if your compiler requires the .h at the end of an #include as I found out long ago with Borland. Quote from: DaveLembke on September 12, 2011, 07:21:31 PM char sex[]; M or F would be a single character. Code: [Select]if (Susan == Mary) should be: Code: [Select]if (&Susan == &Mary) If you want to see if two object references are the same, you need to compare the addresses of the objects. using == will try to call the overloaded comparison operator, but your struct doesn't have one.I usually declare my Chars with char Tag[]; so that if by accident the user enters 2 or more characters such as "Male" it doesnt overflow, and my if statement following it would test the input to make sure it is M, F, m, or f and not FF etc, and if more than 1 character and if the character isnt 1 of 4 valid inputs state the error and request proper input. This input sequence would be nested in a loop whereas if the correct single character is input, the loop would be met and leave loop. But yes BC your correct in your statement that it was ok for a single character input. I should have commented on why I suggested that. Quote from: DaveLembke on September 14, 2011, 01:43:37 AM I usually declare my Chars with char Tag[]; so that if by accident the user enters 2 or more characters such as "Male" it doesnt overflow,It won't overflow. but it will fill the input buffer. the proper way to fix this in a C++ program would be to actually use C++, rather than C, constructs. In this case, strings: Code: [Select]#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string inputline; for(;;) { string charinput; cout << "\nPlease input a letter: "; cin >> charinput; while (!isalpha(charinput[0])) { cout << "You have entered an invalid input, please input a letter: "; charinput.clear(); cin >> charinput; } transform(charinput.begin(), charinput.end(),charinput.begin(), ::toupper); cout << "first character was " << charinput[0] << endl; if(charinput[0]=='q' || charinput[0]=='Q') break; } return 0; } In this case one could just take the first character of the string. This would allow people to enter "FEMALE" or "Male" or "Man" or "Femme" or "Filly" or something crazy like that. Or, of course, one could at that point check for first character, but presumably the input prompt would make it clear what they should do. Quote and my if statement following it would test the input to make sure it is M, F, m, or f and not FF etc, and if more than 1 character and if the character isnt 1 of 4 valid inputs state the error and request proper input.There is of course one problem THOUGH, and that would be if they input characters past the number that you declared for the array. Also, in the example above, I just uppercase the entire string before checking the first character, so I would only need to check for M or F, and not m and f. I didn't mention any of this, since it had absolutely nothing to do with their issue. |
|