LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2003, 06:40 PM   #1
ottbr
LQ Newbie
 
Registered: Jan 2003
Posts: 8

Rep: Reputation: 0
C++ newbie question.


I am attempting to write a program with a function in it that when first called, takes in a string, and then prints out the first word of that string. Then, if sent a NULL as an argument in the next call, it will print out the second word. Doing so until the end of the sentence. If sent a new string, rather than null, it will begin again with the new string, and print out the first word of that string.

My main problem right now is keeping the string sent stored in such a way that it isn't lost after you exit the function, that way, if you send it a NULL the next time you call the function, it still remembers the last string it was sent.

I have tried static character arrays, and dynamic allocation.

If anyone has any ideas or suggestions, I would welcome them.

here is an example of what I am trying to do, hopefully the code isn't too messy:

#include <iostream>
#include <cstdlib>
using namespace std;

void string_eater(char* string);

int main(void)
{
char string[] = "Hello World.";

string_eater(string);
string_eater(NULL);

return 0;
}

void string_eater(char* string)
{

static int count;
static char* stored_string = NULL;

if(string != NULL) //All right I wasn't sent a NULL, so I have to start all over again..
{
count = 0;
stored_string = *string; //There is a new string, time to store it!
}


for(; *(stored_string + count) != ' ' && *(stored_string + count) != '\0'; count++)
cout << *(stored_string + count);

cout << endl;

if(*string == NULL)
cout << "I am all out of string, and you didn't give me a new one!" << endl;

}
 
Old 02-10-2003, 08:57 PM   #2
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
Declare the "char* stored_string" and "int count" as the global variables rather than doing so locally in the "string_eater" function.
 
Old 02-11-2003, 03:17 PM   #3
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Declaring them static has the same effect, except that it restricts their scope to within the function.

stored_string = *string

is dodgy - you're assiging a (char) to a (char *). You'll need to think more carefully about that part of the program.

Btw, code is much easier for us to read if you put it between {code} and {/code} tags (except use square brackets).

Alex
 
Old 02-11-2003, 03:20 PM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Cool

Like these: [code][/code]
 
Old 02-12-2003, 03:11 AM   #5
Ivan Lee
LQ Newbie
 
Registered: Feb 2003
Posts: 7

Rep: Reputation: 0
think of STL vector
 
Old 02-15-2003, 07:19 PM   #6
Wondre
LQ Newbie
 
Registered: Jan 2003
Location: SoCal
Distribution: SlackWare 10
Posts: 17

Rep: Reputation: 0
Make a local static pointer to string. Assign a dynamically allocated new string equal to your incomming, and work on that.

Or

make a static string, longer than you will ever need. Then copy the incoming string to it (strcpy()?), and then work from that stored string.

Copying the incoming string to a local pointer is bad, because there is no storage associated with that pointer, if you dont do something like "stored_string = new char[128];"

Last edited by Wondre; 02-15-2003 at 07:29 PM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Newbie Question. Please Help imfineru Linux - Newbie 8 08-20-2005 05:09 AM
Apache newbie question.. (very newbie question) tarballed Linux - Newbie 1 02-07-2003 08:41 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 12:53 PM
RE: Samba question pls help {Newbie question} Radiouk Linux - Networking 4 06-03-2002 06:40 PM
Security question {newbie question} Radio Linux - Security 3 05-17-2002 06:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration