Like Tree9Likes

errors

This is a discussion on errors within the C++ Programming forums, part of the General Programming Boards category; Here is a simple code which is supposed to convert a double to an ascii string and then an int ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    errors

    Here is a simple code which is supposed to convert a double to an ascii string and then an int to an ascii string by making use of an overloaded function, but when I compile the code I get a whole lots of errors.. and I can't understand why...

    Code:
    #include<iostream>
    #include<conio.h>
    #include<ctype.h>
    #define cout std::cout
    #define endl std::endl
    
    char *toascii(int i);
    char *toascii(double f);
    
    void main ()
    {
    	char *s;
    	
    	double a = 12.3;
    	s = toascii(a);
    	cout<<"float = "<<s;
    	int i=55;
    	s = toascii(i);
    	cout<<"int = "<<s;
    }
    
    char *toascii(double f)
    {
    	char *s[5];
    	gcvt(f,5,s);
    	return s;
    }
    
    char *toascii(int i)
    {
    	char *s[5];
    	itoa(i,s,2);
    	return s;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    This is wrong, even if it works:
    Code:
    #define cout std::cout
    #define endl std::endl
    You probably wanted to write:
    Code:
    using std::cout;
    using std::endl;
    This:
    Code:
    void main ()
    should be:
    Code:
    int main()
    Note that gcvt and itoa are non-standard.

    Besides this, you probably declared s in your functions wrongly (do you really want s to be an array of 5 pointers to char?), then you are returning a pointer to a local variable, plus the pointer is of a wrong type to begin with.

    Oh, and you should have posted your error messages.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by laserlight View Post
    you should have posted your error messages.
    #include<iostream>
    #include<conio.h>
    #include<ctype.h>
    using cout std::cout
    using endl std::endl

    char *toascii(int i);
    char *toascii(double f);

    int main ()
    {
    char s[5];

    double a = 12.3;
    s = toascii(a);
    cout<<"float = "<<s;
    int i=55;
    s = toascii(i);
    cout<<"int = "<<s;
    }

    char *toascii(double f)
    {
    char s[5];
    gcvt(f,5,s);
    return s;
    }

    char *toascii(int i)
    {
    char *s[5];
    itoa(i,s,2);
    return s;
    }

    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(5): error C2143: syntax error : missing ';' before 'std::cout'
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(5): error C2873: 'cout' : symbol cannot be used in a using-declaration
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(5): error C2143: syntax error : missing ';' before 'using'
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(5): error C2371: 'std::cout' : redefinition; different basic types
    1> c:\program files\microsoft visual studio 10.0\vc\include\iostream(26) : see declaration of 'std::cout'
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(7): error C2143: syntax error : missing ';' before 'std::endl'
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(7): error C2873: 'endl' : symbol cannot be used in a using-declaration
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(7): error C2144: syntax error : 'char' should be preceded by ';'
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(7): error C2365: 'std::endl' : redefinition; previous definition was 'function'
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(7): fatal error C1903: unable to recover from previous error(s); stopping compilation
    Last edited by juice; 02-06-2012 at 02:53 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    Quote Originally Posted by juice
    1>c:\documents and settings\shri\my documents\visual studio 2010\projects\data\data\data.cpp(15): error C2296: '&' : illegal, left operand has type 'double'
    This does not seem consistent with the code that you posted. Anyway, fix what I told you to fix, then post the resulting code and corresponding error messages.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    And I know that this might not be appreciated but I had no other option so I also tried to execute the code in Turbo C++ compiler and it reports a "declaration terminated incorrectly" error for the two prototypes of toascii(), along with a couple of more errors..

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,297
    You still don't bother to try to understand error messages from your compiler, eh?

    How about you

    1) list the error messages from your compiler

    2) try to explain why YOU think your code triggers them.

    This is not a forum for asking questions when you are too lazy to help yourself. And you have received considerable help here over some months, so it is only reasonable that people expect you to move beyond the baby phase.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by laserlight View Post
    fix what I told you to fix, then post the resulting code and corresponding error messages.
    I have edited the post.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by grumpy View Post
    You still don't bother to try to understand error messages from your compiler, eh?

    How about you

    1) list the error messages from your compiler

    2) try to explain why YOU think your code triggers them.

    This is not a forum for asking questions when you are too lazy to help yourself. And you have received considerable help here over some months, so it is only reasonable that people expect you to move beyond the baby phase.
    Well maybe I am in the baby phase but everyone is in the baby phase when he begins.
    And you can't call me lazy cause I tried everything that I knew (even the Turbo C++ compiler) , but it didn't help..

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,297
    Quote Originally Posted by juice View Post
    Well maybe I am in the baby phase but everyone is in the baby phase when he begins.
    And you can't call me lazy cause I tried everything that I knew (even the Turbo C++ compiler) , but it didn't help..
    I have no problems with people starting in the baby phase.

    But you have a post history over several months, and a post count approaching 300. It is not unreasonable that you might present some evidence of having learned from the advice you have received in that time. You have not.
    rags_to_riches and Salem like this.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    Quote Originally Posted by juice
    Well maybe I am in the baby phase but everyone is in the baby phase when he begins.
    That's true. I suggest that you start afresh on C++ by working through Accelerated C++.

    The problems with your program are due to a lack of a proper foundation in C++. I could help you fix these problems, but you won't understand why I do what I do without that foundation.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by laserlight View Post
    I could help you fix these problems, but you won't understand why I do what I do without that foundation.
    Please tell me whats wrong, maybe I would understand, and just in case if I don't, I have the book that you recommended...

    Quote Originally Posted by grumpy View Post
    I have no problems with people starting in the baby phase.

    But you have a post history over several months, and a post count approaching 300. It is not unreasonable that you might present some evidence of having learned from the advice you have received in that time. You have not.
    If you are suggesting about the void main, than I am sorry I admit that I was lazy, I wanted to try to debug my code using turbo c++ as the last resort and so I didn't wanted to take the pain of writing int main and then wrinting another return statement at the end of the code. Am sorry.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    Quote Originally Posted by juice
    Please tell me whats wrong, maybe I would understand
    Your whole approach is wrong: why did you reach for #define instead of a using declaration? Why are you using pointers to char when std::string will suffice?

    Quote Originally Posted by juice
    just in case if I don't, I have the book that you recommended...
    So, if you think you understand, you're going to leave that book on the shelf and continue learning rubbish?

    Quote Originally Posted by juice
    If you are suggesting about the void main, than I am sorry I admit that I was lazy, I wanted to try to debug my code using turbo c++ as the last resort and so I didn't wanted to take the pain of writing int main and then wrinting another return statement at the end of the code. Am sorry.
    void main is hardly the problem. Look, I posted two examples of using declarations. This is the sort of thing you would have encountered in a "hello world" program, or maybe something just a little more advanced than that. You managed to get it wrong even after looking at my example, and was apparently unable to spot the problem despite the helpful error message.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,297
    No, I was not only referring to void main(). Turbo C++ has always accepted int main(), BTW, as well as void main(). Other posters have listed other problems in your code, but you don't appear to have even realised that.

    What I'm referring to is you repeatedly asking questions that you have either asked before, or that are very very closely related to questions you have asked before. You also exhibit a propensity to ask for help immediately when a compiler issues a complaint, rather than trying to understand the complaint. Those characteristics suggest that, despite the effort of several other posters in answering your questions, you are not progressing, you are not learning.

    Based on those observations one might potentially describe you as stupid, lazy, or both. In only characterising you as lazy, I was giving you some benefit of the doubt.
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by laserlight View Post
    why did you reach for #define instead of a using declaration?

    I don't know about the using declaration as yet and I didn't wanted to take the pain of writing std:: everytime I use cout so I tried #define and it worked.

    Quote Originally Posted by laserlight View Post
    So, if you think you understand, you're going to leave that book on the shelf and continue learning rubbish?
    Alright I'll read that book.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by grumpy View Post
    What I'm referring to is you repeatedly asking questions that you have either asked before, or that are very very closely related to questions you have asked before. You also exhibit a propensity to ask for help immediately when a compiler issues a complaint, rather than trying to understand the complaint. Those characteristics suggest that, despite the effort of several other posters in answering your questions, you are not progressing, you are not learning.
    I don't know how is this question in anyway related to any of my earlier question, cause I have only started learning C++ as of yesterday, and this is possibly my first post in C++.

    Quote Originally Posted by grumpy View Post
    You also exhibit a propensity to ask for help immediately when a compiler issues a complaint, rather than trying to understand the complaint.
    Yes maybe I do have the propensity of asking questions but thats not because I am lazy. Trust me, I do everything I could before putting it up. And you people know more than I do so I expect you to help me right away without wasting anymore time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with errors
    By sublime in forum C Programming
    Replies: 11
    Last Post: 11-11-2005, 02:23 PM
  2. 100+ errors
    By Ryu++ in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2005, 08:53 AM
  3. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  4. Dev and errors
    By Moffesto in forum C++ Programming
    Replies: 0
    Last Post: 06-22-2002, 12:17 AM
  5. these are the errors
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 09:16 PM

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