Thread: Noob - Program Keeps Crashing

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    9

    Noob - Program Keeps Crashing

    I am trying to create a program where it will read in the student name, nationality, and grades, and calculate their tuition fee based on those information.

    I began to write a bit of code by I have ran into some problems. When I try to run my code, the compiler says "warning C4700: local variable 'name' used without having been initialized" but continues to run the code. Then the program shuts down. What is happening?

    In addition, on my fifth line I have "char *firstName;". I know that char* represents a pointer, but what does that mean and why can't I just use a string?

    Code:
    #include <iostream>
    using namespace std;
    
    struct Student {
    	char *firstName;
    	char *lastName;
    	bool canadianship;
    	int grades[10];
    };
    
    Student newStudent[10];
    
    void addFirstName() {
    	char *name;
    	cout << "Student Name: ";
    	cin >> name;
    	newStudent[0].firstName = name;
    }
    
    
    int main() {
    	addFirstName();
    	cout << newStudent[0].firstName;	
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can just use a string. In fact for your program to have any hope of working, you must use a string. In order to use a string, you do have to bring in the string library with #include <string>.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Thank you for the quick reply. How about why my program keeps crashing?

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You need to allocate space for you pointers... char *name is only a pointer. You need to allocate the memory for the names...

    Code:
    void addFirstName() {
    	char *name = new char[64];
    	cout << "Student Name: ";
    	cin >> name;
    	newStudent[0].firstName = name;
    }
    That should fix the problem
    -- Will you show me how to c++?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by icu222much View Post
    Thank you for the quick reply. How about why my program keeps crashing?
    Because you seem to think "string" is spelled "char *"?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Thank you.

    Sorry, i thought char * was just another way of declaring a string.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Sorry, i thought char * was just another way of declaring a string.

    It is. Well, sort of.

    Using char* for strings is the C way of doing it, but it works in C++ and is still used by many tutorials/books/instructors. Using the C++ string class is generally preferred in C++, so if you can use that, do it.

    If you can't, then you need to allocate space for your char* string. An easy solution is to use a plain char array instead of just a pointer. Then you can allocate space for the string by setting the number of characters in the array. That restricts to a maximum string size, though. Another solution is to use a dynamic array allocated with new[] and destroyed with delete[]. If you're going to do that, though, you really should just use the string class.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Gotcha! Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. another noob program
    By rodrigorules in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2005, 11:22 PM
  3. Please find an error in my noob program
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:55 PM
  4. my noob program doesnt work
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 11:40 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM