Thread: Utter newb?: Program crashes after input

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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