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 07-04-2005, 01:19 AM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
soo stupid, what am i doin???


hey all i cant believe i cant get this working:

<code>

char test[80];
cout << "enter a Full Sentence: ";
cin.getline(test,80);

<code>

the getline DOES NOT WORK???? , and program execution just continues with the next line of code after getline.

i have also tried:
cin.read(test, 80);

but this required that i type exactly 80 chars before input stream exits.

WHat am i doin wrong?? soo stupid i know

Josh
 
Old 07-04-2005, 01:39 AM   #2
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
just a side note, i am running a g++ compiler on a Fedora Core 3 .
THanks.

JUst need to capture user input, in full sentences.

Josh
 
Old 07-04-2005, 10:11 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Try use C++ strings rather than C char arrays
Code:
#include <iostream>

using namespace std;

int main(int argc, char * argv[]) {
    string test;
    cout << "enter a Full Sentence: ";
    getline(cin, test);
    cout << "You entered: \"" << test << "\"\n";
    return 0;
}
 
Old 07-04-2005, 01:51 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Re: soo stupid, what am i doin???

Quote:
Originally posted by blizunt7
hey all i cant believe i cant get this working:

<code>

char test[80];
cout << "enter a Full Sentence: ";
cin.getline(test,80);

<code>

the getline DOES NOT WORK???? , and program execution just continues with the next line of code after getline.

i have also tried:
cin.read(test, 80);

but this required that i type exactly 80 chars before input stream exits.

WHat am i doin wrong?? soo stupid i know

Josh
It would help if you posted a complete program. The following code works for me:
Code:
#include <iostream>

int main() {
  using namespace std;

  for (int i=0; i < 3; ++i) {
    char test[80];
    cout << "enter a Full Sentence: ";
    cin.getline(test,80);
    cout << test << endl;
  }
}
Of course if you type more than 80 characters, this will give you a buffer overrun and a big security hole, so you're better off using a C++ string object.

Which version of GCC/c++ are you using? In case you don't know, GCC-2.96 (http://gcc.gnu.org/gcc-2.96.html) was released by Red Hat, is not a GNU project, has lots of iostream-related bugs, and isn't even a C++ compiler, so I would not be very surprised if it didn't compile that code properly.
 
Old 07-04-2005, 01:56 PM   #5
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
thank you, im not too happy right now.

I tired this as its own program, just the few lines and it worked, i entered a sentence and it output to screen fine.

BUt when i put this in my code, the get sentence is totaly skipped over????

i have other cout statemetns, and cin statements to capture single word inputs, (no spaces)
and then i have this:

string test;
cout << "sentence";
getline(cin, test);
cout << "Entered sentence was : " << test << endl;

:
:
more code.....

What is my problem???

WHy is this code (to enter a sentence) totaly skipped????
thanks you
 
Old 07-04-2005, 02:26 PM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I can see nothing wrong with this snippet of code. Thus it is likely that the problem lies elsewhere in your program.

As previously stated, please post a complete program that fails, and the version of the C++ compiler that you're using.

The only other idea that springs to mind is that you might be using ctrl+D (EOF) to end the input stream after the first input; this would close standard input and allow nothing further to be read from it.
 
Old 07-04-2005, 03:16 PM   #7
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
sorry, for not uncluding all the code. There is my function that is called from main:

int main(void)
{

system("clear");
renewItem();

//initData(); // file will read file, and input into ADT at this point
initMenu();


return 0;
}


Everything else works fine, except for this single sentence input????
I am using gcc version 3.4.2
Thanks !!!!!



Code:
void newItem()
{
    char cont;
    char number[80], weightDwt[80], weightGram[80];
    char gold[80], description[80], price[80];


    ofstream fout;  // object for writing to file itemList
    fout.open("data.gp",ios::app);
    if (!fout.good())
    {
        cout << "Error Creating file\n";

    }
    assert (!fout.fail());

    cout << "Last item number is " << lastItem+4000 << endl;
    cout << "Continue with next number (Y/N)? ";
    cin >> cont;  cout << endl;

    char next[80];
    if (cont == 'Y'||'y')
    {
        int nextItem = lastItem+1+(4000);
        //cout << "nextItem: " << nextItem << endl;

        sprintf(next,"%d",nextItem);  // convert from int to char
        item[++lastItem].setNumber(next);

        // send to file number of next item
        fout  << item[lastItem].getNumber() << ":";
    }
    else
    {   // DONT USE THIS??????
        cout << "Enter number you would like for the next Item: ";
        cin >> number; cout << endl;

    }

    cout << "Enter Weight in DWT: ";
                                      
 cin >> weightDwt; cout << endl;
    item[lastItem].setDwt(weightDwt);
    fout << item[lastItem].getDwt() << ":";
    double dDwt = atof(weightDwt);  // double value of weightDwt char array

    double weightG = dDwt*1.55; // Gram conversion


    gcvt(weightG,6,weightGram);// ?????? Convert double to char


    item[lastItem].setGram(weightGram);
    fout << item[lastItem].getGram() << ":";

    cout << "Enter gold type (10/14/18/22/24): ";
    cin >> gold; cout << endl;
    item[lastItem].setGold(gold);
    fout << item[lastItem].getGold() << ":";

//HERE IS MY ISSUE (I HAVE TRIED THIS EXTERNAL FUNCTION< STILL NO GOOD)
    getSen();
/*
string test;
cout << "Enter a sentence: ";
getline(cin, test);
cout << "your entered: " << test << endl;
*/

    //item[lastItem].setDescription(description);
    fout << item[lastItem].getDescription() << ":";

    double bid;
    double pDwt;
    cout << "Enter Price per DWT so I can calculate bidding price: ";
    cin >> pDwt; cout << endl;

    bid = dDwt*pDwt;

    cout << "Calculated bid = " << bid << endl;
    double roundBid = ceil(bid)-.01;

    cout << "Rounded bid = " << roundBid << endl;

 gcvt(roundBid,8,price);// ?????? Convert double to char

    item[lastItem].setPrice(price);
    fout << item[lastItem].getPrice() << endl;

    fout.close();
 
Old 07-04-2005, 04:46 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
My guess is that your problem lies with using a cin >> something before doing the cin.getline. cin >> blah generally leaves the line feed in the buffer, so at the point that you use a cin.getline() it immediately reads that line feed and returns.

You could try using cin.getline() twice to clear the buffer, or use a cin.ignore(1); // Possibly need 2 for Windows as there is likely a carriage-return/linefeed combo of characters.
 
Old 07-04-2005, 09:41 PM   #9
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
wow that ignore worked, thanks!!!!

josh
 
  


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
HD issues - what am I doin wrong? tkman Linux - Hardware 2 05-14-2005 03:01 PM
i am soo new ..... bendersm Linux - Newbie 5 04-11-2005 05:58 AM
Why is my Redhat Linux soo slow huh83 Linux - Newbie 12 09-27-2004 09:55 AM
Reinstallation: Doin' it right! mattp Linux - Newbie 40 03-08-2004 10:20 PM
newbie soo confused edman Linux - General 3 12-18-2000 07:02 PM

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

All times are GMT -5. The time now is 12:35 PM.

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