Click to See Complete Forum and Search --> : {just starting to learn c++} questions on easy stuff
Fandelem
12-10-2000, 09:01 AM
sorry, just decided to learn c++ a few days ago, so I went online and grabbed some free book (Thinking in C++ by Bruce Eckel) - I have no prorgamming experience, but I don't think it should be too hard to understand.. although, I'm trying to follow some of his example, and they aren't working for me (or my variants, either..)
here is my code (which is almost identical to that in the book):
#include <iostream>
using namespace std;
int main() {
int secret = 15;
int guess = 0;
while(guess != secret) {
cout << "guess the number: ";
cin >> guess;
}
cout << "Yup." << endl;
}
when I try to compile it..
[root@Server code]# gcc test.cpp -o test
/tmp/ccDNLtm2.o: In function `main':
/tmp/ccDNLtm2.o(.text+0x26): undefined reference to `cout'
/tmp/ccDNLtm2.o(.text+0x2b): undefined reference to `ostream: http://www.linuxnewbie.org/ubb/redface.gifperator<<(char const *)'
/tmp/ccDNLtm2.o(.text+0x37): undefined reference to `cin'
/tmp/ccDNLtm2.o(.text+0x3c): undefined reference to `istream: http://www.linuxnewbie.org/ubb/redface.gifperator>>(int &)'
/tmp/ccDNLtm2.o(.text+0x46): undefined reference to `endl(ostream &)'
/tmp/ccDNLtm2.o(.text+0x50): undefined reference to `cout'
/tmp/ccDNLtm2.o(.text+0x55): undefined reference to `ostream: http://www.linuxnewbie.org/ubb/redface.gifperator<<(char const *)'
/tmp/ccDNLtm2.o(.text+0x60): undefined reference to `ostream: http://www.linuxnewbie.org/ubb/redface.gifperator<<(ostream &(*)(ostream &))'
collect2: ld returned 1 exit status
[root@Server code]#
It almost looks like I haven't included something, but that's just my uneducated guess.. what should I try? :}
thanks in advance {I look forward to asking questions here frequently :}
~kyle
Muzzafarath
12-10-2000, 09:06 AM
Use g++ instead of gcc to compile C++ programs.
Mikenell
12-10-2000, 09:06 AM
I'm not totally sure about this, but I'm pretty sure your using the C compiler to compile C++ code. Try gpp or g++ instead of gcc and see if that works.
Mikenell
Fandelem
12-10-2000, 09:23 AM
hot damn, that's what it was =)
well, easy enough problem and easy enough answer, thanks guys for the super fast response; look forward to more questions over winter break =)
have a great day/night
~kyle
Fandelem
12-10-2000, 09:41 AM
actually, i lied, i do have another question =)
i know how to do this in visual basic, but i haven't really figured out how to do it in C++
here is my code so far (yes, I know it's wrong :}
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> words; //declare 'words' as a vector
ifstream in("system.cpp"); //open for reading
string word; //declare variable
while(getline(in, word)) //while input is not null
words.push_back(word);
for(int i = 0; i < words.size(); i++)
cout << word << "There are " << i << " spaces";
}
basically, what i'm trying to accomplish is take any given file, and as long as it isn't EoF, count how many whitespaces there are
what happens when this code executes:
[root@Server code]# ./calcwhitespace
There are 0 linesThere are 1 linesThere are 2 linesThere are 3 linesThere are 4 linesThere are 5 lin
esThere are 6 linesThere are 7 linesThere are 8 linesThere are 9 lines[root@Server code]#
How can I have my code calculate this [b]internally and then just display the last line?
1. Also, (thanks to anyone who puts up with me answering all these newbie questions) I'm confused about:
is this the same:
ifstream in("system.cpp"); //open for reading
as:
ifstream in(system.cpp); //notice no quotes
basically, when do I need to put quotes? (I know for strings), but what about other things? is there some explicit rule where anything variable-wise requires no quotes?
2. and what is the deal with when I see on some people's "hints" section for programming, they have:
char stringA[11] = "abcde"
and some people have
string stringA
I guess - I think the [11] means a max of 11 characters it can hold, right? But why the char before it? and in the latter example, what am I really defining by it? just a string that could possibly have buffer overflows?
okay, thats all the questions i've had after reading 3 or 4 chapters.. thanks for any answers :}
regards,
~kyle
A_Lawn_GNOME
12-10-2000, 11:26 AM
cout << "There are " << words.size() << " white spaces.";
a string is a fancy char array (The []'s)
Fandelem
12-10-2000, 05:40 PM
wow, I changed it to:
cout << endl << "There are " << words.size() << " spaces" << endl;
and it worked!!
I don't understand though, why would I want to print words.size instead of i (which was the counter..)?
thanks for your help though, now it works :}
A_Lawn_GNOME
12-10-2000, 05:57 PM
You're counting something that has already been counted. The for loop wasn't needed (tho I guess I forgot to mention that before but I guess you got the idea).
Size returns the internal number that keeps track of the size.
To simplify things (a lot), the program will substitute cout << words.size(); with cout << "9";
Stuka
12-11-2000, 01:48 AM
The specific reason your program wouldn't compile at first is that C doesn't understand that <iostream> is a header file - it expects <iostream.h> - IIRC, you can use gcc or g++, but w/gcc you have to specify flags for certain C++ only things (like no .h extension). As for quotes, any time that a C++ function expects a char* or string argument, and you want to pass it a string literal, you have to use quotes. Here, of course, I would recommend using a symbolic constant at the beginning, e.g. const char *INFILE = "system.cpp" to simplify your life if you need to change things later. A string variable is, in essence, a glorified char array. It is dynamically expandable, and has operations to add, insert, remove, etc. strings, chars, and char*s. It also supports lots of cool STL things like iterators and algorithms. And the array declaration - ALL arrays are declared the same - <data type><array name>[array size]. That, I hope, answers that question.
Fandelem
12-11-2000, 04:20 AM
As for quotes, any time that a C++ function expects a char* or string argument, and you want to pass it a string literal, you have to use quotes. Here, of course, I would recommend using a symbolic constant at the beginning
what if I wanted something like:
cout << "What file would you like to open for reading: ";
int word1;
cin >> word1;
ifstream in(word1); //open for reading
string word; //declare variable
while(getline(in, word)) //while input is not null
I know this doesn't work - but I think you might get the idea - What if I wanted to prompt a user for a file to open, then put their input into a variable (i know how to do this much) and then have ifstream open that variable - I know it's not using quotes, because that's a "literal" as you mentioned.. but I tried without quotes and it doesn't work (although I don't know if it just doesn't work because of other things, hehe)..
Strike
12-11-2000, 04:43 AM
There's nothing wrong with modifying a variable and then using the contents of that variable to declare a new variable. The same thing happened in the "Bonus Question" thread. But you should just declare a ifstream first (like ifstream infile;) and then just call a infile.open(<filename> );.
Fandelem
12-11-2000, 07:07 AM
There's nothing wrong with modifying a variable and then using the contents of that variable to declare a new variable. The same thing happened in the "Bonus Question" thread. But you should just declare a ifstream first (like ifstream infile http://www.linuxnewbie.org/ubb/wink.gif and then just call a infile.open(<filename> );.
sorry if i'm taking this too literally, but here's what I tried:
int main() {
vector<string> words; //declare 'words' as a vector
cout << "What file would you like to open for reading: ";
int word1;
cin >> word1;
ifstream infile; //open for reading
infile.open(word1);
string word; //declare variable
while(getline(in, word)) //while input is not null
words.push_back(word);
cout << endl << "There are " << words.size() << " spaces" << endl;
}
error on compiling:
[root@Server code]# c++ calcwhitespace.cpp -o calcwhitespace
calcwhitespace.cpp: In function `int main()':
calcwhitespace.cpp:14: no matching function for call to `ifstream: http://www.linuxnewbie.org/ubb/redface.gifpen (int &)'
/usr/include/g++-2/fstream.h:67: candidates are: ifstream: http://www.linuxnewbie.org/ubb/redface.gifpen(const char *, int, int)
calcwhitespace.cpp:16: `in' undeclared (first use this function)
calcwhitespace.cpp:16: (Each undeclared identifier is reported only once
calcwhitespace.cpp:16: for each function it appears in.)
I think the book I'm reading assumes I know everything about ifstream.. but I don't - could someone explain it to me or maybe a link where I can read and see some examples?
sorry to bug you more,
~kyle
Strike
12-11-2000, 09:32 AM
The open() method is only defined for string and char * as far as I know. Why are you trying to open an integer filename?
zGoRNz
12-11-2000, 06:51 PM
what he means is when you declare word1 as a int you are saying a number, you mean "string word1;" (w/o the "s)
------------------
Dunt Dunt Duh...
GoRN To The Rescue,
Yet Again
zGoRNz@yahoo.com
aim: GoRNToTheRescue