|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
urgent c++ question
|
Original Message
|
Name: s-men
Date: September 10, 2003 at 12:04:47 Pacific
Subject: urgent c++ question OS: win 2000 pro CPU/Ram: pentium 133mhz
|
Comment: hello, I recently started studing c++. But I have a problem. I want if the user typs stop, the program stops. I want to use it in this way: if (a==stop) goto stop; but it doesn't work. What is the code doing this? It would help me a lot.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: gpp
Date: September 10, 2003 at 12:18:30 Pacific
|
Reply: (edit)What sort of a program is this.. just some kind of repetative loop? Theres usually a little more logic involved than that. simple example.. do{ //get input if(//user enters stop){ break; } } while(true)
I hope the synatax was close.. I dont do much with c anymore.
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: s-men
Date: September 10, 2003 at 12:40:00 Pacific
|
Reply: (edit)I don't know much yet but this is an example of what I want #include <iostream>int main () { start: int a; cout <<"typ 1 to stop";
cin >> a;
if (a==1) goto stop; else goto start; stop: return 0; } but instead of int a, I want to use : char a [4]; si that the user must typ stop instead of 1 but this is what doesn't work: if (a==stop) goto stop; so I looking for a diffrent code that can perform this action I want offcourse this is an simple example but it represents what I want so I can use it in my programs
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: JavaToad
Date: September 10, 2003 at 13:26:49 Pacific
|
Reply: (edit)there is no goto statement in c++. c++ designers recognize that the goto can lead to more problems than it solves. use recursion whenever appropriate, it tightens your code and is very efficient, otherwise resort to standard while loops, for loops, or gpp's do - while example.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: talhakhan
Date: September 10, 2003 at 13:29:06 Pacific
|
Reply: (edit)gotos the way you are using them isn't good programming style ... like the above post said, you should use a do/while loop also using char a[4] is a bit restrictive, the user might enter several characters and overrun the array, perhaps use a larger constant than 4 also, == compares numbers, so if you did a == "stop", this would compare memory address and not the string. use strcmp function, e.g. if this expression is true: strcmp(a, "stop") == 0 then the strings are equal, remember that its case sensitive
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: gpp
Date: September 10, 2003 at 13:33:45 Pacific
|
Reply: (edit)I would advise against goto's in your code, they make code messy, and if this is homework, will make your instructor mad. you could do something like this... string cmnd="";
cin >> cmnd;
if(cmnd.compare("stop") == 0){
//end program
}
again, you should double check syntax because I dont use c much...
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: zeroguy
Date: September 10, 2003 at 14:14:57 Pacific
|
Reply: (edit)that syntax looks fine to me, gpp. Except that I'm not too sure about the string.compare() function, becuase I haven't had too much experience with the string data type, but it looks all right.
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: gpp
Date: September 10, 2003 at 14:20:38 Pacific
|
Reply: (edit)Well, I just dont remember C++ that well, and I'd hate to give someone advice that wont actually work.
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: Rolos
Date: September 10, 2003 at 15:10:49 Pacific
|
Reply: (edit)S-men: In your situation, set up the array of type char. So do this: char a[4]; Now, you can do an error check if the user overflows your array and check if they have entered "stop" by doing the following: if (strlen(a) > 4 || a == "stop") { cout << "You chose to stop!" << endl; cout << endl; return 0; } What the strlen() function does is it returns the actual length of the string to an integer. You can use this quantity for the "if-statement" comparisons. Also many programmers and professionals are against the use of goto. You would produce some confusing "spaghetti code" (and I quote Infinite Recursion) which is terrible for debugging purposes and code readability. So it is a good practice to stay away from the use of goto. - Rolos
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
|
Reply: (edit)Bummer. I was late getting to this post. talhakhan mentioned that a character array of size 4 is a bit restrictive, this can be resolved by using the strlen() function that Rolos outlined above, his if statement stops when "stop" is encountered as well as if the user enters a value that exceeds 4 characters long. You can also use the string data type in the Standard Templates Library that was developed by SGI, much easier and better methods to work with. Don't use gotos, you hurt my feelings. *sniffle* j/k, I just hope I don't come across your code and have to debug it years after you left the company... gotos suck, leads to department wide confusion and a trashing of your program... known as spaghetti code, which is translated into LOST CAUSE. At any rate, Rolos listed the main code for you... I made a few modifications, hopefully this will be clear. Here it is, in all of its standard compliancy... lol - Compiled / Ran on Dev-C++ 4.9.8.0. Enjoy, Infinite Recursion ------ #include <iostream>
using namespace std;
int main (void)
{
char a[4];
cout << "Enter: ";
cin >> a;
if (strlen(a) > 4 || (strcmp(a,"stop") == 0))
{
// STOP encountered, bail out
cout << "You chose to stop!" << endl;
cout << endl;
system("pause");
return 0;
}
// STOP not encountered, keep going
// -- use recursion or while/for loop
// -- to repeat code comparison.
// -- DO NOT USE GOTOS, You hurt my feelings :(
system("pause");
}
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: Don Arnett
Date: September 10, 2003 at 21:28:30 Pacific
|
Reply: (edit)Rolos a == "stop" ?????? Was you sleepy??
I guarantee that won't work in C, and would be flabbergasted if that worked in C++ if 'a' is a char array, although there are ways you could make it LOOK like it works if you make 'a' a char pointer. char *a;
a = "stop"; if (a == "stop") { ...something } This will work, assuming that the compiler optimizes the two 'stop' literals into one. In that case, 'a' will contain the address of the 'stop' literal, so the 'if' will be comparing the value in 'a' (the address of the 'stop' literal) and the address of the 'stop' literal.
Or did C++ sneak in something that I haven't learned yet??
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
|
Reply: (edit)True that the line of code in Rolos' post if (strlen(a) > 4 || a == "stop") will not execute the code in that if block for reasons Don mentions above... that particular line should read something like the one in my prior post that uses the strcmp() function... if (strlen(a) > 4 || (strcmp(a,"stop") == 0)) Although, for all I really know, Rolos may have his on class that processes strings or character arrays differently. I was forced to write one in my college Data Structures class. Base of the matter is if you don't have anything "special" going on, you need to use strcmp() here. Another solutions is if we declared variable types of string using the Standard Template Library... the basic_string data structure has several methods to compare strings. View them here: (http://www.sgi.com/tech/stl/basic_string.html). I listed a few of them below for everyone's "viewing pleasure"... ========================================== template bool operator==(const basic_string& s1, const basic_string & s2) template bool operator==(const charT* s1, const basic_string& s2) template bool operator==(const basic_string& s1, const charT* s2) ========================================== ---My personal favorite is--- template bool operator& s2) This is the simple string comparison. Equivalent to `(s1 == s2)`. In addition it returns whether or not s1 is lexographically lesser than s2. At any rate, that may have been a bit over kill for this particular thread. However, my code in my prior post will get the job done for you using strcmp(). Regards, Infinite Recursion
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
|
Reply: (edit)Argh. Forgot about that code coversion, my apologies... here is that particular segment of my post... I listed a few of them below for everyone's
"viewing pleasure"...
==========================================
template <class charT, class traits, class Alloc>
bool operator==(const basic_string<charT, traits, Alloc>& s1,
const basic_string<charT, traits, Alloc>& s2)
template <class charT, class traits, class Alloc>
bool operator==(const charT* s1,
const basic_string<charT, traits, Alloc>& s2)
template <class charT, class traits, class Alloc>
bool operator==(const basic_string<charT, traits, Alloc>& s1,
const charT* s2)
==========================================
---My personal favorite is---
template <class charT, class traits, class Alloc>
bool operator<(const charT* s1, const basic_string<charT, traits, Alloc>& s2)
Report Offensive Follow Up For Removal
|
|
Response Number 13
|
Name: SN
Date: September 11, 2003 at 07:30:37 Pacific
|
Reply: (edit)"use recursion whenever appropriate, it tightens your code and is very efficient, otherwise resort to standard while loops, for loops, or gpp's do - while example" What??? Since when is recursion more efficient than standard loops? On each recursive call, more information is loaded on to the stack to be able to evaluate whatever it is that the function returns...My understanding is that in most (but not all) cases, recursion is slower and less memory efficient (in runtime) than using regular loops. I don't know the efficiency of goto...But as the "branch always" command is usually implemented in one way or another in all architectures, I would guess that it's actually very efficient...One opcode and one operand. It certainly messes up the structure and stability of the code, so I'm all against its use, but if I were writing the compiler, I don't think it be memory or time intensive to implement. I think we've given this poor soul much more than he/she asked for...Hopefully they've figured out that the == sign doesn't work with strings (even in Java) unless you do something tricky. You can write your own comparison subroutine, or use a string data type and use theirs. *Body slams the dead horse* -SN
Report Offensive Follow Up For Removal
|
|
Response Number 14
|
Name: s-men
Date: September 11, 2003 at 08:32:46 Pacific
|
Reply: (edit)thanks for the info I'm gonna try this out as fast as I can and the reason why I use goto's: I started studing c++ 3 weeks ago on a site but with school I don't had much time to learn more about it. And I didn't came to the ++ part yet, it's the c part that I'm learning now. untill now I had to make the user use 9 or something to stop and I thought that it would be better if they could actualy typ stop. and why I'm using only 4 letters that can be input for a, stop is 4 letters long and it was an example code, if I make a full program I'll make them longer so they can use several commands which could be longer then 4 letters anyways, thanks for the help grtz, s-men
Report Offensive Follow Up For Removal
|
|
Response Number 16
|
|
Reply: (edit)The length of the command to stop doesn't matter really, just set it for one length and use strlen() to get the length of the command and make sure it does exceed the MAX, in this case that would be 4. Since this code doesn't really call another function, using recursion would be overkill. Just use a looping mechanism, for instance: a while loop with break statements in the appropiate place... The efficiency of recursion is ultimately relative to the size of the stack and processing speed. Again, using recursion on this project instead of standard loops would be equivalent to dropping an H-Bomb on a army full of retreating, unarmed taliban riding through the sand dunes on their camels. Oh well, you get the idea... btw, the use of goto is never rendered justified unless you are programming assembly... ;) (Carpet bombs the, otherwise, dead horse) Infinite Recursion
Report Offensive Follow Up For Removal
|
|
Response Number 17
|
Name: Eric
Date: September 11, 2003 at 18:42:13 Pacific
|
Reply: (edit)Rofl @ I.R. ... drop the damn H-bomb... na jp and no I don't have anything against talibans :O I wish I understood what you guys were talking about. I understood some but its still a foreign language to me. f--- you smart people! Bah you know i love you ;D
Report Offensive Follow Up For Removal
|
|
Response Number 18
|
Name: Rolos
Date: September 12, 2003 at 02:44:56 Pacific
|
Reply: (edit)Hahaha. Yeah Don you're absolutely right, I was hammered off of three pots of coffee, about 6 cigars and an hour of sleep going into a 16 hour day replying to this topic in my lab at school at 40 degrees with the A/C blasting and beginning CS students buzzing in the background with their absurdly WRONG ideas. But hey, I was once like that so I shouldn't say anything. I do not know wtf I was thinking actually, now that I see what I had posted, it's really stupid. That is why they have strcmp(). But yeah, I've been slipping up lately. I think this Discrete Math has been really loosening the screws in my head a bit more than they usually are 8) But in retrospect, I believe that you are wrong in a way Don. Because the equivalency check would definitely would work in the Rolos++ language and not C++ 8) (totally joking). Yeah but anyway, I am very sorry *s-men* for stealing your post and steering it in a totally different direction. "Now back to your regularly scheduled programming". - Rolos
Report Offensive Follow Up For Removal
|
|
Response Number 19
|
Name: Rolos
Date: September 12, 2003 at 02:54:00 Pacific
|
Reply: (edit)You know, I shouldn't proof read the stuff I blurt out. I will have stupid typos left in it anyway. It was better the way I had it in the first place. *Grabs the horse and flings it into the hot, burning fires of the Sun*. - Rolos
Report Offensive Follow Up For Removal
|
|
Response Number 20
|
Name: ffxifanatic
Date: September 13, 2003 at 20:10:45 Pacific
|
Reply: (edit)i wouldnt use cin for a string by the way, spaces have a way of screwing them up. Use cin.get(stringname,stringlength); and an ignore() after it. and for disbeleivers, goto maybe good for DOS but its a real hassel in c++
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Programming Forum Home
|
|
|