Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > c++ issues

c++ issues
Thread Tools
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Dec 20, 2002, 09:57 AM
 
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
Reply With Quote
Dec 20, 2002, 10:30 AM
 
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
     
milf  (op)
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Dec 20, 2002, 10:45 AM
 
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
Reply With Quote
Dec 20, 2002, 02:04 PM
 
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.
     
milf  (op)
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Dec 20, 2002, 02:09 PM
 
Nope, I'm using the standard g++ compiler through a terminal window.
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:58 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2