I quit!

This is a discussion on I quit! within the C++ Programming forums, part of the General Programming Boards category; bah just kidding! :P no really my problem is as follow.. im trying to make a program that will ask ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    Angry I quit!

    bah just kidding! :P

    no really my problem is as follow..

    im trying to make a program that will ask 10 different words and then sort them in order.
    if one word should be same as an another one it will not be sorted twice.
    in the same time if user input quit at anytime the app should be able to end task right away...

    my problem is in the quit command...

    im not sure why but the best i can get is to get the app to loop forever?
    even if i enter quit it will always go on..
    quite pathethic..

    any suggestions?

    ho right here is what i tried :

    Code:
    while(str[i]!= "quit" )
    		{
    			for (i=0;i<10;i++)
    			{
    				cout<<"enter a word" <<endl;
    				//getline (str[i],f,'\n');
    				cin >> str[i];
    			}
    		}
    im using codewarrior v7.0 for mac on os X..

    thx in advance..

    Luigi

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You can't compare char arrays using logical operators such as == != etc. You have a few choices:

    1) make a function that cycles through checking each character in the arrays

    2) use strcmp from the string header

    3) make or use a premade string class

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    If you check it out, you can see that he isn't using char arrays. The problem is that the while loop isn't checking the string array correctly.
    Code:
    for (i=0;i<10;i++)
    {
    	cout<<"enter a word" <<endl;
    	cin >> str[i];
    	if (str[i]== "quit")
    		break;
    }
    Try that.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by pianorain
    If you check it out, you can see that he isn't using char arrays.
    There is absolutely no reason that you can make that assumption. All that he's posted is representative of using char arrays. Especially considering the fact hat he was using getline.

    Though yes, he did have a logical error with the loop itself
    Last edited by Polymorphic OOP; 12-01-2002 at 11:11 PM.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ah...my mistake. I was able to make a quickie that only used char arrays and still compiled, so it can be done. However, every time I've used getline, it's been with a stream parameter and a string parameter (with an occasional delimiter parameter). I didn't think it could take a char array as a parameter.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    When you use an array in an expression by itself it returns a pointer to the first element which works with getline.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    //declare and initialize variables
    char input[80] = "start";
    char str[10][80];
    int i = 0;
    
    //accept up to 10 words as input
    //stop before 10 words entered if input is quit  
    while(strcmp(input, "quit") != 0 && i < 10)
    {
       cout<<"enter a word" <<endl;
       cin >> str[i++];
    }

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    in what order do you wont the words to be in acending order
    or decending
    C++ Is cool

  9. #9
    GuestTrauts
    Guest
    Can't you use strcmp(x,y) on strings? If I remember right, that if it is a positive return (NOT 0), then the first one comes after the second one in alphabetical order, if it is negative, then it comes before.

    Try using that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. processes not exiting or quit
    By kryptkat in forum Tech Board
    Replies: 8
    Last Post: 11-13-2006, 12:43 PM
  3. Can't quit my program
    By Elite in forum C Programming
    Replies: 2
    Last Post: 03-04-2005, 06:09 AM
  4. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21