LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Storing Strings (https://www.linuxquestions.org/questions/programming-9/storing-strings-226585/)

ToothlessRebel 09-04-2004 11:28 AM

Storing Strings
 
I am admittedly a newbie, but working on amending that. My newest learning experience is something that has frustrated me in the past, but now I find something fantastic and .... can't make it work!

Trying to use the type
Code:

string
by using
Code:

#include <string>
by using the following code (straight from my class textbook, Alternate Version of Starting Out With C++ 4th Edition) with gcc v3.2.3:

Code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string movieTitle;
movieTitle = "Wheels of Fury";
cout << "I like " << movieTitle << endl;
return 0;
}

When I try and compile I get:

C++/test.cpp:10:2: warning: no newline at end of file

It compiles, and creates the executable, which when ran displays nothing. Why is it not displaying the output as I would expect:

Quote:

I like Wheels of Fury

bruce ford 09-04-2004 11:58 AM

Hi,
I cannot reproduce your problem, your code works for me as expected - I have g++ 3.3.1 though.

Does your program work without using std::string? How do you compile your program?

So long...
bruce

deiussum 09-04-2004 12:08 PM

The warning is because your last line is probably } instead of a blank line. No big deal.

The reason it probably isn't displaying anything is because you are probably running the Linux test program. try typing

which test

And you will probably get the result:

/usr/bin/test

To run your own test program, try executing it like so:

./test

ToothlessRebel 09-04-2004 11:03 PM

To answer bruce ford, no.

Code:

#include <iostream>
#include <string>

int main()
{
 string monster;
 monster = "Zombie";
 cout << "The monster is a " << monster << endl;
}

Produces:

Quote:

C++/monster.cpp: In function `int main()':
C++/monster.cpp:6: `string' undeclared (first use this function)
C++/monster.cpp:6: (Each undeclared identifier is reported only once for each
function it appears in.)
C++/monster.cpp:6: syntax error before `;' token
C++/monster.cpp:7: `monster' undeclared (first use this function)
C++/monster.cpp:8: `cout' undeclared (first use this function)
C++/monster.cpp:8: `endl' undeclared (first use this function)
I can thank deiussum though, I think he may be right. Recompiling the above code with the "using namespace std;" in place, and compiling with "g++ C++/monster.cpp -o C++/monster" the code ran fine.

Thanks a million, I had no idea what the Linux "test" program was.

bruce ford 09-05-2004 03:16 AM

hi rebel,

great that things are solved.
No I didn't mean to take out the 'using namespace std;' line but to use char* instead of string.

Have fun,
bruce


All times are GMT -5. The time now is 01:18 AM.