 |
 |
c++ issues
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status:
Offline
|
|
I'm not sure if it is something that I am doing wrong (probably) or if it is something else, but the code that I am writing is not functioning the way I think it should. Even when I type in code directly from a tutorial, it does not produce the same output as the tutorial says it should.
Can anyone see anything wrong with the bit of code? I'll add comments to try and help clarify what my problems are.
/* Simply copies and returns a string. This part of the code works fine */
char* makeCopy(char * toCopy)
{
char* theCopy = new char[strlen(toCopy) + 1];
strcpy (theCopy, toCopy);
return theCopy;
}
/* Updates the title and ISBN information */
void Collection::Add()
{
char buffer[100];
// the following two are protected members of the Collection class
delete thisTitle;
delete thisISBN;
/* The next lines are where the problems come in. I attempt to read in the string using cin.getline, but something is failing. I check the ios bits, but nothing is set. When I attempt to output buffer after the first cin.getline, a blank is displayed w/ no content. When I do just a simple cin >> buffer, I get what I typed in, but of course the problem with this is that cin only reads up a space. Does anyone have this same problem or see what I am doing wrong */
cout << "Enter the title: ";
cin.getline(buffer, sizeof(buffer), '\n');
thisTitle = makeCopy(buffer);
cout << "Enter the ISBN: ";
cin.getline(buffer, sizeof(buffer), '\n');
thisISBN = makeCopy(buffer);
}
|
|
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
Well, for me,
Code:
#include <iostream.h>
int main()
{
char buffer[100];
cout << "Enter some stuff: ";
cin.get(buffer, sizeof(buffer));
cout << buffer << endl;
return 0;
}
works as expected:
Code:
[untermac:~] banana% g++ -o main main.cpp
[untermac:~] banana% ./main
Enter some stuff: sejilst
sejilst
Your problem could be that you are mistaking the extra blank line caused by the use of getline for your output not working; WHY getline is requiring that extra carriage return I'm not quite sure. I don't know iostreams very well yet
Changing that line to
Code:
cin.getline(buffer, sizeof(buffer));
results in almost the same result:
Code:
[untermac:~] banana% g++ -o main main.cpp
[untermac:~] banana% ./main
Enter some stuff: test
test
As you can see, an extra newline is required because I am using getline instead of get. Using the additional argument '\n' in either function call makes no difference, as that argument is optional and its default is '\n'.
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status:
Offline
|
|
Well, I coded a simple program just to test reading in a string and it worked just fine. I'm still not sure why getline requires the extra return; I don't recall it ever being that way on any other system I've written code on.
I tried changing the line in my actual code from getline() to get() and it went berzerk. It started spitting out random lines and ended with a lovely segmentation fault. I'm not sure how chaning that one line of could cause such complications.
I'll post more of my code when I get home and hopefully someone else can see what the problem is. I'm probably messing something simple up. Oh, the joy of coding.
Thanks for the response.
|
|
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
You're not running this from within Project Builder, are you? CLI text input is kinda funky from within PB.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status:
Offline
|
|
Nope, I'm using the standard g++ compiler through a terminal window.
|
|
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|