#include <iostream>
#include <math.h>
void printName();
void squareRoot();
void clearScreen();
int main ()
{
using namespace std;
int choice = 0;
char exitChoice = '\0';
clearScreen();
do
{
cout << "Type in a number that corresponds to one of the options below";
cout << "\nthen hit 'enter' or 'return'.";
cout << "\n\t\t\t****MENU****";
cout << "\n\t\t\t(1) First name";
cout << "\n\t\t\t(2) Find sqaure root";
cout << "\n\t\t\t(3) option 3";
cout << "\n\t\t\t(4) exit";
cout << "\n";
cin >> choice;
switch (choice)
{
case 1:
clearScreen();
printName();
break;
case 2:
clearScreen();
squareRoot();
break;
case 3:
clearScreen();
cout << "You picked option three!\n\n";
break;
case 4:
exitChoice = 'y';
break;
default:
clearScreen();
cout << "~~~~~~~~~~~~ PICK AGAIN! ~~~~~~~~~~\n\n";
}// end switch
}// end loop
while (exitChoice != 'y');
return 0;
}
void printName()
{
using namespace std;
char buffer[80] = {'\0'};
cout << "Enter your name ";
cin >> buffer;
cout << "Hey " << buffer << "!\n\n";
return;
}
void squareRoot()
{
double a = 0;
std::cout << "enter a number: ";
std::cin >> a;
a = sqrt(a);
std::cout << "The square root is " << a << "\n\n";
return;
}
void clearScreen()
{
for (int i = 0; i < 100; i++)
{
std::cout << "\n\n\n\n\n";
}
return;
}
4 replies to this topic
#1
Posted 29 December 2011 - 02:42 PM
hey guys, im just now jumping into c++ and i wrote a little something just goofing around and testing out all the stuff ive learned so far. in the program below ive found a bug when option one is selected and the user types in more than his first name, like firstname space lastname. the string is supposed accept any character including spaces up to the specified amount of characters, but if you copy paste, compile and run it this does not happen. I cant figure out where this bug is coming from and i dont know how to use xcode to debug so im looking for some experienced help on this one.
[SIGPIC][/SIGPIC]
|
|
|
#2
Posted 29 December 2011 - 03:30 PM
Your issue is that cin is formatted input, it will break at the first name and continue. If you want a first and last name, you will have to use something such as cin.getline() for this and separate them afterwards if required, or just list the full name as accepted.
Further, since the string format is not entirely relevant to your issue you may as well turn it in to an std::string object - cstrings are cumbersome. You will have to use getline instead of cin.getline if you were to switch to std::string however
Try to start there and report back, the cin.getline documentation is here: istream::getline - C++ Reference
getline() getline - C++ Reference
Further, since the string format is not entirely relevant to your issue you may as well turn it in to an std::string object - cstrings are cumbersome. You will have to use getline instead of cin.getline if you were to switch to std::string however
Try to start there and report back, the cin.getline documentation is here: istream::getline - C++ Reference
getline() getline - C++ Reference
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 29 December 2011 - 04:43 PM
well alexnder i used cin.get() and i dont know if this is the same thing but it didnt work when i used it.
heres the amended function
heres the amended function
void printName()
{
using namespace std;
char buffer[80]={'\0'} ;
cout << "Enter your first name: ";
cin.get(buffer,79);
clearScreen();
cout << "Hey " << buffer << "!\n\n";
return;
}
[SIGPIC][/SIGPIC]
#4
Posted 29 December 2011 - 06:30 PM
A pseudo-interaction with your program would look like this:
Personally I would accept choice as a string (getline()), and compare it as a string. If someone does not enter a proper integer (as choice is an integer) it can also go in to an infinite loop. You should maybe employ some error checking (there're facilities for that with std::cin, however that is not my forté).
Prompt: Enter choice: (you enter 1 and hit <enter>) Buffer: 1 is removed due to the formatted cin input, newline from enter still remains Prompt: Enter name: (newline goes here automatically, is whitespace, is ignored, continues in possibly infinite loop)
Personally I would accept choice as a string (getline()), and compare it as a string. If someone does not enter a proper integer (as choice is an integer) it can also go in to an infinite loop. You should maybe employ some error checking (there're facilities for that with std::cin, however that is not my forté).
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 29 December 2011 - 11:46 PM
how do i test to make sure
or better yet how do i make absolute sure that the newline character goes away after hitting enter?
choiceis an int?
or better yet how do i make absolute sure that the newline character goes away after hitting enter?
[SIGPIC][/SIGPIC]
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









