Thread: This never stops for some reason

  1. #1
    Shadow12345
    Guest

    This never stops for some reason

    This never breaks
    Code:
    #include <iostream>
    #include <vector>
    #include <stdlib.h>
    #include <fstream>
    
    
    using namespace std;
    
    typedef vector<char*> CV;
    CV CharacterVector;
    
    ofstream fout;
    
    
    int main(void) {
    	fout.open("crap.db");
    
    	int Number = 0;
    
    	for( ; ; )  {{//stupid smily face arg
    
    	CharacterVector.push_back(new char[50]);
    
    	cout << "enter a character array "<< endl;
    
    	cin.getline(CharacterVector[Number], 50);
    
    	if(!atoi(CharacterVector[Number]) == 0) {
    
    		cout << "You have chosen to quit" << endl;
    
    		break;
    
    	}
    	else {
    		fout << CharacterVector[Number] << endl;
    		Number++;
    	}
    	}}
    
    	fout.close();
    	return 0;
    }
    My problem is with the atoi function, it never detects a '0' being entered.


    This is kind of sad, I have been programming for a year now and I am still doing stupid stuff like this.

    What are your opinions, do you think someone who has been programmign for a year should still be doing stupid stuff like this?
    Last edited by Shadow12345; 09-02-2002 at 09:28 AM.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    use tabs and ........ to make your code readable
    hello, internet!

  3. #3
    Shadow12345
    Guest
    it's a small program you can read it

  4. #4
    First off:

    for( ; ; ) {{ //<------- What the hell are two curly-braces doing here?
    [...]
    }} //<------- And the closing ones here?

    It doesnt help that its very difficult to figure out whats inside what braces as you should indent a little differently (i.e. More).

    if(!atoi(CharacterVector[Number]) == 0)

    is possibly the strangest way i've ever seen this done. Why not:

    if (atoi(CharacterVector[Number]) != 0) // ?

    Also, if you're only checking a single character for a 0, why not just use the value of it? Try this:

    Code:
    int main(void)
    {
    	fout.open("crap.db");
    
    	int Number = 0;
    
    	for( ; ; )
    	{
    		CharacterVector.push_back(new char[50]);
    
    		cout << "enter a character array "<< endl;
    		cin.getline(CharacterVector[Number], 50);
    
    		if (CharacterVector[Number] == 48) //48 is the ASCII value of 0. Alternately: if (CharacterVector[Number] == '0') would work
    		{
    			cout << "You have chosen to quit" << endl;
    			break;
    		}
    		else
    		{
    			fout << CharacterVector[Number] << endl;
    			Number++;
    		}
    	}
    
    	fout.close();
    	return 0;
    }
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #5
    Shadow12345
    Guest
    I don't see how it makes it harder to read
    it's easier for me
    that's why i do it

    you can't convert from const char to int

    --------------------Configuration: atoi - Win32 Debug--------------------
    Compiling...
    atoi.cpp
    C:\atoi\atoi.cpp(28) : error C2446: '==' : no conversion from 'const int' to 'char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    C:\atoi\atoi.cpp(28) : error C2040: '==' : 'char *' differs in levels of indirection from 'const int'
    Error executing cl.exe.

    atoi.exe - 2 error(s), 0 warning(s)

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Shadow12345
    I don't see how it makes it harder to read
    it's easier for me
    that's why i do it
    the mark of a novice programmer.


    write logical, well-organized, well-commented code.
    hello, internet!

  7. #7
    Shadow12345
    Guest
    ok wow thank you for pointing out the obvious...like i care if i'm a novice, why don't you just help me with the problem at hand.


    what about the stricmp function
    how do you use that?
    I am trying ot look for it in mybook

  8. #8
    Shadow12345
    Guest
    Okay this works:
    Code:
    
    #include <iostream>
    #include <vector>
    #include <stdlib.h>
    #include <fstream>
    
    
    using namespace std;
    
    typedef vector<char*> CV;
    CV CharacterVector;
    
    ofstream fout;
    
    
    int main(void)
    {
    fout.open("crap.db");
    
    int Number = 0;
    
    for( ; ; )
    {
    CharacterVector.push_back(new char[50]);
    
    cout << "enter a character array "<< endl;
    cin.getline(CharacterVector[Number], 50);
    
    if (!stricmp(CharacterVector[Number], "0"))
    {
    cout << "You have chosen to quit" << endl;
    break;
    }
    else
    {
    fout << CharacterVector[Number] << endl;
    Number++;
    }
    }
    
    fout.close();
    return 0;
    }
    Thanks lightatdawn for trying to help even though you said hell to me

    Moi...I AM a novice programmer for all intensive purposes, pointing that out doesn't do any good...ever...

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    i'm not flaming you as a novice programmer; i am giving you a way to improve yourself!
    hello, internet!

  10. #10
    Sorry, didnt notice that part.

    try this:

    if (CharacterVector[Number][0] == 48)

    or this:

    if (CharacterVector[Number][0] == '0')

    instead. It will work.

    Or, alternatly, your origional atoi test will work if you want to test the whole string, not just the first element. You just dont want the !.

    if (atoi(CharacterVector[Number]) == 0)

    strcmp would work too. strcmp returns 0 if the comparison is TRUE.

    if (strcmp(CharacterVector[Number]), "0") == 0)

    P.S. The indentation I used is much more readable to others. Obviously you do whatever you like best, its your code, but its a good habit to get into indenting like I posted.

    /* Edit */
    >>even though you said hell to me

    Heh, sorry if that offended. Thats just me. Just trying to help.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  11. #11
    Shadow12345
    Guest
    I wasn't flaming you either, I really wasn't. I know that putting double curly braces isn't good practice, but I was still at the stage where I just wanted the program to do its assigned task. Cleaning up the code is something I like doing afterwards.

    How long, in your opinion, do you have to be actively programmign in order to not be considered novice. I mean a lot of people might think I am a god, but then other people really might think I am just a stoopid newb. I've been programming C++ for about a year. I'm getting into OpenGL (so far I can onlymake a triangle that moves about the screen when you press the arrow keys. I tried writing my own base code but NeHe's is far better. nehe.gamedev.net)

    I also plan to really get into Visual C++ w/MFC, and maybe Java and C#. I know Visual Basic, but that doesn't count as a language to me.

  12. #12
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Shadow12345

    How long, in your opinion, do you have to be actively programmign in order to not be considered novice.
    i think it's a stupid thing not worrying about. where do you draw the "novice" vs "non-novice" line? continue coding and you'll get better and better, simple as that. i'm not such a hot coder either , but i have one piece of advice for you: adopt an indentation scheme and use it as you write whenever you write. it really doesn't take longer to write, and it makes your code easier to read and dissect and debug in the future, and you might think "i'm only writing this 20 line program", but what if you decide to reuse the code in something larger or simply want to refer back to it later?
    hello, internet!

  13. #13
    >>How long, in your opinion, do you have to be actively programmign in order to not be considered novice

    Forever. Theres always someone better than you and always someone worse. I mean, I'm a 'newb' compared to the likes of adrianxw, sayeh, and the like, and yet I've been programming for about seven years.

    The learning never stops.

    /* Edit */

    moi makes a good point, worth listening to. Indentation is one of the most simple things you can do that is so important.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  14. #14
    Shadow12345
    Guest
    Yeah that makes sense. I read in Code Complete that the better programmers are the ones that hit the books, not the ones that have X amount of experience (although experience is necessary). Software (and technology in general) is changing so much that 'hitting the books' does seem like a good idea. About the indentation scheme. I really like this scheme:

    if(something == true) {
    dosomething();
    }

    As opposed to

    if(something == true)
    {
    dosomething();
    }

    Is that really difficult to read? The latter one bugs the snot out of me, it really does.

    I dont' like programmers that try to make themselves out to be better, or are doing it to impress people. I really like programmign when you are achieving something and working through problems and then finally getting some satisfaction out of getting them solved. Then again I don't think people who want to program to impress people last very long because they don't have the dedication to work through problems.

  15. #15
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Shadow12345
    Yeah that makes sense. About the indentation scheme. I really like this scheme:

    if(something == true) {
    dosomething();
    }

    As opposed to

    if(something == true)
    {
    dosomething();
    }

    both are good schemes; i use the former. just remember to indent dosomething(); in either case!!

    but "{{" is not an indentation scheme
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Stops Compiling
    By tyler4588 in forum C++ Programming
    Replies: 8
    Last Post: 08-02-2003, 10:20 PM
  2. Getting tab stops to work
    By jverkoey in forum Windows Programming
    Replies: 4
    Last Post: 07-06-2003, 02:27 PM
  3. My thread stops for no reason
    By lectrolux in forum C Programming
    Replies: 7
    Last Post: 05-21-2003, 07:56 AM
  4. Why do this loop stops..!
    By jawwadalam in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2002, 09:24 PM
  5. sort algorithm stops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-16-2001, 10:55 AM