GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Dec-2008, 17:12
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Problem assigning strings to pointers.


Hi im designing a madlibs program for school and need a little bit of help.
What i want to do is beable to assign each userinput to the string pointer.
CPP / C++ / C Code:
#include<iostream>
#include<string>


using namespace std;


char *str[15] = { "Liquid", "Vehicle", "Adverb", "Exclamation", "Verb ending in -ing",
			"Part Of Body", "Vehicle", "Occupation", "Article og Clothing",
			"First Name (Male)", "First Name (Male)", "Noun", "Verb", "Sily Noise",
			"Part of the Body"};

int main()
{
	for (int i = 0; i < 15; i++)
	{
		cout << str[i] << endl;
		//getline(cin, str[i]);
		
	}
    return 0;
}
  #2  
Old 07-Dec-2008, 18:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem assigning strings to pointers.


Quote:
Originally Posted by lief480
Hi im designing a madlibs program for school and need a little bit of help.
What i want to do is beable to assign each user input to the string pointer.
But you don't have a string pointer. You don't have any strings. You have an array of pointers to char. Each pointer is initialized to point to a string literal somewhere in program memory. You can make it point to somewhere else, but wherever it points, there must be something for it to point to. It is illegal to try to store something in the memory that holds string literals. Although some compilers appear to let you do it, the behavior is undefined and is asking for trouble.

Why not just use an array of std::string objects?
CPP / C++ / C Code:
#include<iostream>
#include<string>

using namespace std;

string str[15] = {
    "Liquid",            "Vehicle", "Adverb",     "Exclamation",         "Verb ending in -ing",
    "Part Of Body",      "Vehicle", "Occupation", "Article og Clothing", "First Name (Male)", 
    "First Name (Male)", "Noun",    "Verb",       "Sily Noise",          "Part of the Body"
};

int main()
{

    for (int i = 0; i < 3; i++) {
        cout << "Old string: ";
        cout << str[i] << endl << endl;
        cout << "Enter a new string: ";
        getline(cin, str[i]);
        cout << "New string: " << str[i] << endl << endl;

    }

    cout << "Recap:" << endl;
    for (int i = 0; i < 15; i++) {
        cout << "  str[" << i << "] = " << str[i] << endl;
    }
    return 0;
}

Here's the output for a run. I made the first loop short so that I could test without entering all 15 things:
Code:
0 Old string: Liquid Enter a new string: 12-year old Scotch You entered: 12-year-old Scotch 1 Old string: Vehicle Enter a new string: Starship You entered: Starship 2 Old string: Adverb Enter a new string: eerily You entered: eerily Recap: str[0] = 12-year-old Scotch str[1] = Starship str[2] = eerily str[3] = Exclamation str[4] = Verb ending in -ing str[5] = Part Of Body str[6] = Vehicle str[7] = Occupation str[8] = Article og Clothing str[9] = First Name (Male) str[10] = First Name (Male) str[11] = Noun str[12] = Verb str[13] = Sily Noise str[14] = Part of the Body
  #3  
Old 07-Dec-2008, 19:44
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Problem assigning strings to pointers.


yeah that would work lol wasnt even thinking about that. Thanks for your help
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of Pointers to strings clp C++ Forum 6 28-Nov-2008 21:47
Accessing strings using pointers anitha09 C Programming Language 5 17-Jan-2008 09:47
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:47.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.