Thread: I have no idea what's going on

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    I have no idea what's going on

    I'm extremely new to C++... This is my code:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char string(50);
        cout << "Enter file name:";
        cin.getline (string, 50, '/0');
    
    }
    I am getting the following error/warning messages:

    C:\Users\Bayne\HEytl\main.cpp|10|error: invalid conversion from 'char' to 'char*'|

    C:\Users\Bayne\HEytl\main.cpp|10|error: initializing argument 1 of 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]'|

    C:\Users\Bayne\HEytl\main.cpp|10|warning: overflow in implicit constant conversion|

    What's wrong here?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The implicit conversions are happening because you have a typo. You do not declare an array with the size in parentheses.

    Code:
    char string[ 50 ];

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First you should not use the name "string" for a variable name in C++. This is the name of a standard C++ class.

    Second: To define a C-string variable you need to use brackets "[]" not parentheses "()".

    Jim

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by theworldluvsu
    Code:
    int main()
    {
        char string(50);
        cout << "Enter file name:";
        cin.getline (string, 50, '/0');
    ...
    If you are programming in C++ there is no need to use C style strings. Look at using the std::string class. A good introduction to this can be found here.

    If you are serious about learning C++, learn how to code C++ style and avoid list C or "almost C" programs (as Bjarne would say).
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Part of the problem is that there is a class named string in the namespace std, which you so casually import into the global namespace. And obviously you cannot reserve a name for a variable that is already reserved for a type (ie, a class).
    Use std::string instead of char and std::getline instead of std::cin.getline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    Part of the problem is that there is a class named string in the namespace std, which you so casually import into the global namespace. And obviously you cannot reserve a name for a variable that is already reserved for a type (ie, a class).
    Use std::string instead of char and std::getline instead of std::cin.getline.
    Quote Originally Posted by jimblumberg View Post
    First you should not use the name "string" for a variable name in C++. This is the name of a standard C++ class.
    "string" is not, never has been, and won't be a reserved word. A hint comes from syntax highlighting: reserved words are highlighted blue or some other color. What can happen is name clashing; but that only happens if OP actually types #include <string> AND using namespace std; one of which he did not do here. "string" is a perfectly acceptable identifier, especially after you pick up the habit of avoiding using namespace whatever;

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That depends. It's also a perfectly meaningless identifier for a variable. If it's called string, that doesn't give you any information you didn't already have. In the OP's example, fileName would have been a good name.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    "string" is not, never has been, and won't be a reserved word. A hint comes from syntax highlighting: reserved words are highlighted blue or some other color. What can happen is name clashing; but that only happens if OP actually types #include <string> AND using namespace std; one of which he did not do here. "string" is a perfectly acceptable identifier, especially after you pick up the habit of avoiding using namespace whatever;
    When you realize that a header can implicitly include others, you realize that it isn't safe to use "using namespace std;" and any reserved names for a class. On this compiler, the OP got away with it, but who can say what happens with other compilers?
    Also some things others haven't commented on:

    char string(50);

    is valid syntax, and created a single char named "string" and initializes it with the values 50. So it is valid syntax, but it isn't logically correct.
    Also, '/0' should be '\0'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    When you realize that a header can implicitly include others, you realize that it isn't safe to use "using namespace std;" and any reserved names for a class. On this compiler, the OP got away with it, but who can say what happens with other compilers?
    You're completely wrong on this. There are lists of reserved words available and string isn't on that list. If you don't trust this source, you can find others. I defy you to find "string" on any list. The whole point of using namespaces (ETA: not to be confused with "using namespace std;" but I'm implying saner usage) is preventing name clashing. If string were a reserved word, it wouldn't need to be in a namespace.
    Last edited by whiteflags; 07-18-2011 at 01:33 PM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I didn't say it was a reserved keyword. It's a reserved name for a class living inside the std namespace.
    It so happens the OP imports the entire std namespace and dumps it into the global namespace, which makes the word "string" a dangerous thing to use as a variable name.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Valid identifiers are never reserved for any purpose.

    Let's get really crazy:
    Code:
    #include <string>
    #include <iostream>
    void foo()
    {
       std::string string;
       string = "hello\n";
       std::cout << string;
    }
    It might be meaningless but this does compile. So "reserved" is really the wrong thing to say.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you say so.
    My point is, that if you add "using namespace std" in there and use string, you might just run into compile errors on some compilers.
    If you include "string", you will get an error on all standard compliant compilers.
    If you're unlucky and and haven't included "string", you might still get compile errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Indeed, the problem is "using namespace std;" and nothing else.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Absolutely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    If you say so.
    My point is, that if you add "using namespace std" in there and use string, you might just run into compile errors on some compilers.
    If you include "string", you will get an error on all standard compliant compilers.
    If you're unlucky and and haven't included "string", you might still get compile errors.
    No you won't.

    I agree that using identifiers from the standard library is best avoided, but C++ name lookup tends to do the right thing. It is allowed to have two identifiers with the same name where one broad scope includes the narrower scope of the other identifier. In this case, where there is overlap, the narrower scope is preferred.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any idea?
    By Moony in forum C Programming
    Replies: 9
    Last Post: 07-05-2006, 02:47 AM
  2. Got idea but is this possible?
    By Hugo716 in forum Game Programming
    Replies: 10
    Last Post: 06-06-2006, 05:41 AM
  3. Have no idea (please help)
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 01-25-2006, 02:16 PM
  4. IDEA: Tic-Tac-Toe
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 09-02-2002, 05:25 PM
  5. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM