Thread: Utter newb?: Program crashes after input

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    Utter newb?: Program crashes after input

    I am completely new to programming. Im currently working out of 'C++ without fear' by Brian Overland as it is recommended on this site as the first book in a sequence to learn c++. I have an idea for a game I want to design, and after each of the exercises in the book I try to take what I learned and make a small program with characteristics similar to something I might do in the program. This extremely simple program crashes right after input. This is what I am trying to do:

    1. Declare an array of strings with 1 element
    2. Call a function to get the user to input a name into the first(and only) element of a local array of strings
    3. Have that function return the value of the first element of that array
    4. Assign the value of that element to the first element of the array declared in step 1
    5. For testing purposes, output the value of that element.

    While typing this out, a couple things occurred to me. I could just pass by reference the address of that first element, assign the value to that address and then change that function to a void function right? Further, since I am just using one element of an array of strings, I could just use a string, instead of an array, right? However, this isnt ultimately what I want to do and I will be using more elements once I get beyond this utter newb stage. All of this aside, the fact that this simple program keeps crashing despite no compiler warnings (devc++) is annoying me, b/c its obviously something Im doing thats completely wrong. Anyways, here is the code.

    Code:
    #include <iostream>
    #include <ctype.h>
    
    using namespace std;
    
    char getname ();
    
    int main(){
        char *name[1];
        *name[0]=getname ();
        cout<<name[0];
        cin>>*name; //COMMENT - this is just to pause the output so I can see it
        
    }
    
    char getname (){
         char *name[1];
         cout<<"Enter your name: ";
         cin.getline(name[0],15);
         return *name[0];
         }
    Last edited by deductible; 12-13-2008 at 08:11 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Several things to look at here. char *name[1] gives you an array of pointers of length one. So name[0] is a pointer, and that's good. However, it currently points to New Jersey; in order to use it, you'll have to make point to some memory inside your computer.

    Next, in the function getname you tell getline that you want to read up to fifteen characters. Provided you fix the first part, you will have room to store them in name[0]. But then, you only return the first letter of the name to main; the rest is thrown away. Note also that this char *name[1] is not the same as the char *name[1] inside main.

    Lastly, it's not clear what you intend cin >> *name to be.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Several things to look at here. char *name[1] gives you an array of pointers of length one. So name[0] is a pointer, and that's good. However, it currently points to New Jersey; in order to use it, you'll have to make point to some memory inside your computer.

    Next, in the function getname you tell getline that you want to read up to fifteen characters. Provided you fix the first part, you will have room to store them in name[0]. But then, you only return the first letter of the name to main; the rest is thrown away. Note also that this char *name[1] is not the same as the char *name[1] inside main.

    Lastly, it's not clear what you intend cin >> *name to be.
    Ok thanks alot for your reply. The concept of pointers is a weird one for me, one moment I seem to understand them, the next moment Im clueless. I havent quite wrapped my head around them and I am not on the same page with the guy who is writing this book. I have adjusted the program and it seems to work. At the moment I wrote it i understood fully why it did what it did, now its just cloudy. Anyways, if any other total noobs dig up this post from a search, this is what I changed it too.

    Code:
    #include <iostream>
    #include <ctype.h>
    
    using namespace std;
    
    char getname (char name[1]);
    
    int main(){
        char name[1];
        
        name[0]=getname(&name[0]);
        
        for(int x=0;x<=(strlen(name)-1);x++)
        cout<<name[x];
        cin>>name;
        
    }
    
    char getname (char name[1]){
         
         cout<<"Enter your name: ";
         cin.getline(name,15);
         return name[0];
         }
    Also I was surprised this worked, b/c this book says i need to #include <string> header file, but apparently I do not for the strlen() function?

    edit: I also do not fully understand why the first version caused a console crash rather than a compiler error?
    Last edited by deductible; 12-13-2008 at 09:52 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deductible
    Also I was surprised this worked, b/c this book says i need to #include <string> header file, but apparently I do not for the strlen() function?
    You should #include <cstring> for the std::strlen() function. You can sometimes get away with not including the correct headers since other headers that you do include may include them, but relying on such a thing could mean that your code may break with another standard library implementation, or a different version of the same standard library implementation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Ok, definitely in over my head, but I will do so. I assume everything you just said will make sense soon.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    > I also do not fully understand why the first version caused a console crash rather than a compiler error?

    Because you didn't have any errors in your code. It just didn't do what you wanted it to do. (That is to say, it's not an error to write to the memory address pointed to by some weird pointer -- when getting down and dirty with the hardware, it happens all the time, or so I've been told.) The C mantra, which C++ has inherited somewhat, is "That's your job." It's your job (as programmer) to make sure everything points to where it's supposed to point, and it's the program's job to do what it's told.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching all input and sending it to another program
    By RedWind in forum C Programming
    Replies: 13
    Last Post: 09-15-2008, 12:51 PM
  2. Calling delete Crashes my Program
    By thetinman in forum C++ Programming
    Replies: 19
    Last Post: 10-13-2007, 03:07 AM
  3. Replies: 1
    Last Post: 08-11-2006, 05:44 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM