![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
|
As requested by Big-K I'm reposting my previous question.
I've been working on this (very simple) code: #include <iostream>
int main()
{
std::string name;
int age;
std::string ansr;
std::string new_name;
std::string ans;
std::cout << "what's your name?\n";
std::cin >> name;
if (name != "Broax")
new_name = name;
if (name == "Broax")
{
std::cout << "Hello, Broax! You are 20 years old right?\n";
ansa:
std::cin >> ansr;
if (ansr == "Yes")
{
std::cout << "Yeah! I'm good!\n";
goto end;
}
else if (ansr == "No")
{
std::cout << "Liar! I'm never wrong!\n";
goto end;
}
else
{
std::cout << "Just say Yes or No, man!\n";
goto ansa;
}
}
else
std::cout << "Hmmm... Nice to meet you, " << name <<"! =) How old are you?\n";
std::cin >> age;
std::cout << "Ok, " << name << " you're " << age <<"... I'll keep that in mind, now!\n\nDo you want to test me?\n";
ansb:
std::cin >> ans;
if (ans == "Yes")
std::cout << "Lol! =p You're name is " << new_name << " and you're " << age << " see... I didn't forget!\n";
else if (ans == "No")
std::cout << "Nice to see that you trust me! =D\n";
else
{
std::cout << "Say what?\n";
std::cout << "C'mon... give me a straight answer! Yes or No?\n";
goto ansb;
}
end:
std::cout << "Well then... Now that we're over with this it's time to say goodbye, ok?\nI'll miss you and stuff!\n\n";
std::cout << "And thus we end our little tale of love and adventure! Please press return to get back to your life, being now certain that your names is " << new_name << " and that you are " << age << "!\n";
std::cin.get()
return 0;
}But now I have 2 small questions to present you with. a) Quoting Uman Quote:
b) If a person enters a two (or more) word name, the program dies a horrible death. What's up with that and how should I fix it? Thanks for the attention, everyone! Cheers!
__________________
PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA PORTUGALPORTUGA |
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
#include <iostream>
using namespace std; //it is easier to use this instead of std::bla.
int main()
{
string name;
int age;
string ansr;
string new_name;
string ans;
bool stay = false;
cout << "what's your name?\n";
cin >> name;
if (name != "Broax")
new_name = name;
if (name == "Broax")
{
cout << "Hello, Broax! You are 20 years old right?\n";
do
{
stay = false;
cin >> ansr;
if (ansr == "Yes")
{
cout << "Yeah! I'm good!\n";
}
else if (ansr == "No")
{
cout << "Liar! I'm never wrong!\n";
}
else
{
cout << "Just say Yes or No, man!\n";
stay = true;
}
}while(stay);
}
else
{
cout << "Hmmm... Nice to meet you, " << name <<"! =) How old are you?\n";
cin >> age;
cout << "Ok, " << name << " you're " << age <<"... I'll keep that in mind, now!\n\nDo you want to test me?\n";
do
{
stay = false;
cin >> ans;
if (ans == "Yes")
cout << "Lol! =p You're name is " << new_name << " and you're " << age << " see... I didn't forget!\n";
else if (ans == "No")
cout << "Nice to see that you trust me! =D\n";
else
{
cout << "Say what?\n";
cout << "C'mon... give me a straight answer! Yes or No?\n";
stay = true;
}
}while(stay)
cout << "Well then... Now that we're over with this it's time to say goodbye, ok?\nI'll miss you and stuff!\n\n";
cout << "And thus we end our little tale of love and adventure! Please press return to get back to your life, being now certain that your names is " << new_name << " and that you are " << age << "!\n";
cin.get()
return 0;
}about your second question, see this thread with a similar problem: http://www.programmingforums.org/for...ead.php?t=5108 Last edited by OpenLoop; Jul 27th, 2005 at 11:44 AM. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Here is the unswer for part
a) #include <iostream>
#include <string>
#include <stdlib> // to use std::cin.get();
// miss type variable name (i.e ansa or ansb should be ansr)
// forgot to include string heder file.
int main()
{
std::string name;
int age;
std::string ansr;
std::string new_name;
std::string ans;
std::cout << "what's your name?\n";
std::cin >> name;
if (name != "Broax")
new_name = name;
if (name == "Broax")
{
std::cout << "Hello, Broax! You are 20 years old right?\n";
ansr:
std::cin >> ansr;
if (ansr == "Yes")
{
std::cout << "Yeah! I'm good!\n";
break;
}
else if (ansr == "No")
{
std::cout << "Liar! I'm never wrong!\n";
break;
}
else
{
std::cout << "Just say Yes or No, man!\n";
return ansr;
}
}
else
std::cout << "Hmmm... Nice to meet you, " << name <<"! =) How old are you?\n";
std::cin >> age;
std::cout << "Ok, " << name << " you're " << age <<"... I'll keep that in mind, now!\n\nDo you want to test me?\n";
std::cin >> ans;
if (ans == "Yes")
std::cout << "Lol! =p You're name is " << new_name << " and you're " << age << " see... I didn't forget!\n";
else if (ans == "No")
std::cout << "Nice to see that you trust me! =D\n";
else
{
std::cout << "Say what?\n";
std::cout << "C'mon... give me a straight answer! Yes or No?\n";
return ans;
}
std::cout << "Well then... Now that we're over with this it's time to say goodbye, ok?\nI'll miss you and stuff!\n\n";
std::cout << "And thus we end our little tale of love and adventure! Please press return to get back to your life, being now certain that your names is " << new_name << " and that you are " << age << "!\n";
std::cin.get();
return 0;
}b.) I forgot how to ignore the rest of the input after the space or try to limit the input token... hmmm actually the compiler will ignore the input after the space... I'll take a look at it later... I don't have compiler on this computer. please correct me if it is incorrect. Thanks, Harry Last edited by Mjordan2nd; Jul 27th, 2005 at 12:32 PM. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
pr0gm3r, please use code tags as noted in the forum guidelines.
The use of "goto" is discouraged because it can lead to executing code far beyond the programmer's comprehensible view. It makes the code very hard to understand and very had to maintain. Nevertheless, it is not "evil" as many high-priests and gurus will maintain. After all, in the emitted machine code which results, every branch is a goto. Instead of using goto at the higher levels, we tend to use the natural boundaries that the code will automatically make its way to when it completes a loop or executes (or skips) the code in some block of conditional statements. It is entirely possible to get into a deeply nested section of code and come upon the requirement to get the hell out in a hurry (possibly in response to some real time event). The non-goto way to do this is to have a flag or flags that, when set, cause loops and such to be exited with a "break". In reality, the "break" generates a goto to an effective label at the end of the block. As a matter of fact, the "continue" statement generates a goto to an effective label at the beginning of the block. At any rate, one might have to wend their way through numerous flag tests and break statements to unnest. The use of a goto is possibly justified under those circumstances. Quite a bit of the Linux kernel code uses gotos, particularly in the error handling portions. My personal opinion is that it is overused. That's entirely beside the point, as I'm not managing that project. Your code, modified to avoid the use of gotos and labels, is below. Some possible sticking points are noted. #include <iostream>
#include <string>
using namespace std;
int uhOh (string badNews)
{
cerr << badNews << endl;
return EOF;
}
int main()
{
string name;
string answer;
string newName;
int age = 20;
bool badAnswer = true;
cout << "What's your name?\n";
// This function (>>) will delimit its input on whitespace. Check the
// manual for these input functions to determine what you need.
// Cin is an input stream and has numerous other methods associated
// with it, such as .get (), .getline (), and so forth. The extraction
// operator (>>) is probably the most problematic function it has. Its
// primary attraction is simplicity. The string class also has a getline
// method that is distinct from the stream getline.
// Replace this ==> cin >> name;
getline (cin, name);
if (!cin.good ()) return uhOh ("Failure on input");
// This is superfluous, as it's the same condition as the else clause
// for the following test. As a matter of fact, "new_name" is not
// needed, as the name will be in "name".
/*
if (name != "Broax")
new_name = name;
*/
if (name == "Broax")
{
// Everthing here if name is "Broax"
cout << "Hello, Broax! You are 20 years old right?\n";
while (badAnswer)
{
getline (cin, answer);
// Don't forget to test the input stream -- exercise
// Realize that there are case issues here. "yes" will fail.
if (answer == "Yes")
{
cout << "Yeah! I'm good!\n";
badAnswer = false;
// The else-if and else portions will automatically be skipped
// goto end;
}
else if (answer == "No")
{
cout << "Liar! I'm never wrong!\n";
badAnswer = false;
// goto end;
}
else
{
cout << "Just say Yes or No, man!\n";
}
} // Will fall out of this while loop once a good answer is given
} // This is the end of the name == Broax part. If we came here we
// will skip the entire else section and automatically wind up at the end
else // This is if the name is NOT Broax. It needs braces to block all the
// instructions. Otherwise, only the first statement is considered to
// be part of the block.
{ // <== put that brace there
cout << "Hmmm... Nice to meet you, " << name <<"! =) How old are you?\n";
// The following is asking for a number. If the user (maliciously
// or carelessly) enters something that can't be construed as a valid
// number (like "Broax"), the input stream is going to go into a fail state
// and stay there until cleared. The program is essentially going to run off
// into the weeds and barf on its shoes.
cin >> age;
// So test it
if (!cin.good ()) return uhOh ("Shoulda used another input method");
badAnswer = true;
cout << "Ok, " << name << " you're " << age
<< "... I'll keep that in mind, now!\n\nDo you want to test me?\n";
while (badAnswer)
{
// Even if the extraction operator worked properly, the ENTER keycode
// will be left in the stream and will be read on the next input, which
// is rarely what one wants. Consequently, we dump it, and clear any
// possible error.
cin.sync ();
cin.clear ();
getline (cin, answer);
// It is perfectly valid to leave out braces when the code block is
// a single line. It is, however, bad practice, until one accrues
// more experience. It's a mistake waiting to be made.
if (answer == "Yes")
{
cout << "Lol! =p You're name is " << name
<< " and you're " << age << " see... I didn't forget!\n";
badAnswer = false;
}
else if (answer == "No")
{
cout << "Nice to see that you trust me! =D\n";
badAnswer = false;
}
else
{
cout << "Say what?\n";
cout << "C'mon... give me a straight answer! Yes or No?\n";
// goto ansb;
}
} // Will exit this while when a good answer is finally given
} // This is the end of the "not Broax" section
// Regardless of the name, we wind up here, no "goto" necessary.
cout << "Well then... Now that we're over with this it's time to say goodbye, ok?\nI'll miss you and stuff!\n\n";
cout << "And thus we end our little tale of love and adventure! Please press return to get back to your life, being now certain that your names is "
<< name << " and that you are "
<< age << "!\n";
cin.get();
return 0;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Pr0gm3r, I will assume that you're not very familiar with C++. a "return" statement is used for functions to return values and variables. You might be confused between "return" in Qbasic which is similar to goto and the C++ "return". Here's an example on how to use return:
int sum(int x, int y)
{
int z = x + y;
return z; //returns the sum of x and y to the caller
}
void main()
{
int w;
w = sum(3, 2); //sum will return 5
cout<<w; //output is 5
}btw, the end code tag should be [/code] |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Despite the fact that Microsoft uses it in tons of examples, the use of "void main (blah, blah)" is considered by almost everyone to not comply with the standard. It seems that there is a misplaced semicolon in all the legalese that can result in an interpretation of "The compiler implementor can do it any other way he pleases, as well."
Almost any implementation of C/C++ startup code will return some value to the executive even if main is declared as void. This value may be seen by the user as a failure of the application even if it performs as specified.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4
![]() |
right from the horse's mouth (Benjamine Stroustrup)
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it even been C. See the ISO C++
standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming
implementation accepts
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
A conforming implementation may provide more versions of main(), but they
must all have return type int. The int returned by main() is a way for a
program to return a value to "the system" that invokes it. On systems that
doesn't provide such a facility the return value is ignored, but that doesn't
make "void main()" legal C++ or legal C. Even if your compiler accepts "void
main()" avoid it, or risk being considered ignorant by C and C++ programmers.
In C++, main() need not contain an explicit return statement. In that case,
the value returned is 0, meaning successful execution. For example:
#include<iostream>
int main()
{
std::cout << "This program returns the integer value 0\n";
}
Note also that neither ISO C++ nor C99 allows you to leave the type out of a
declaration. That is, in contrast to C89 and ARM C++ ,"int" is not assumed
where a type is missing in a declaration. Consequently:
#include<iostream>
main() { /* ... */ }
is an error because the return type of main() is missing.Last edited by Ancient Dragon; Jul 27th, 2005 at 12:39 PM. |
|
|
|
|
|
#8 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
~corrected the code tags in pr0gm3r's post.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
Oh ya... I just figure out my mistake... sry I was doing it in hurry... hehehe...
but thank you for your advise OpenLoop on the return keyword. I shouldn't use "return ansr;" because it will cause an error because no function will catch the value. I was thinking that statement is inside of a functions. maybe I should just delete the "return ansr;" btw is ansa another variable or miss spell ? Broax, I'm just assuming it is miss spell ok ^____^.
__________________
-- pr0gm3r |
|
|
|
|
|
#10 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|