LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-09-2006, 05:56 AM   #1
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
simple C++ question


What shall I do if I don't want to define string size?

i.e
Code:
#include <iostream>
#include <cstring>
using namespace std;

int main()
        {
                cout << "prints N times the message you want" << endl;
                int count=0;
                cout << "How much times do you want to write it?" << endl;
                cin >> count;
                cout << "What do you want to write?" << endl;
                char what[10];
                cin >> what;
                for (int i=0; i<=count; ++i) {
                                                cout << what << endl;
                                             }
                return 0;
        }
if I enter a string which is > 10, I create an overflow, that will cause unexpected results.
If I use char* pStr, it gets me segmantation fault, or maybe I've done this wrong?

suggestion?
 
Old 07-09-2006, 06:07 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If you're using C++, why not just use the string class?
 
Old 07-09-2006, 06:09 AM   #3
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Nylex
If you're using C++, why not just use the string class?
can you write a small example?
I just don't find this in the book I got to learn the language.
 
Old 07-09-2006, 06:19 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Certainly . If you just want to read in a string with no spaces, e.g. just a word:

Code:
#include <iostream>
#include <string>

int main()
{
  string myString;
  cout << "Enter a word: ";
  cin >> myString;

  cout << "You entered: " << myString << endl;
  return 0;
}
If you want to enter a string that will contain spaces, like a sentence, you want to use getline instead:

Code:
cout << "Enter a string: ";
getline(cin, myString);
Which book are you using, by the way?
 
Old 07-09-2006, 06:33 AM   #5
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Nylex
Which book are you using, by the way?
add me on msn aitzk@hotmail.com

Ohh... and, Thanks!

[edit]
Is there any reason it doesn't work?
Code:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main()
        {
                cout << "prints N times the message you want" << endl;
                int count=0;
                cout << "How much times do you want to write it?" << endl;
                cin >> count;
                cout << "What do you want to write?" << endl;
                string myString;
                getline(cin,myString);
                for (int i=0; i<=count; ++i) {
                                                cout << myString << endl;
                                             }
                return 0;
        }

Last edited by itz2000; 07-09-2006 at 06:37 AM.
 
Old 07-09-2006, 11:11 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I know the reason, but for the life of me can't remember the work around. Whats happening is that you are reading from the standard input for the count and it leaves the newline '\n' on the standard input then when using getline it takes the newline off this is why you are not seeing anything.
I havn't got my books at hand at the minute, but I will have a little look around the net and post back.

[edit]
Didnt take long
after you call to read the count add
cin.ignore(1,'\n');

what this does is looks 1 characters in the standard input looking for a newline which it then disguards all the input upto and including the newline.

edited ignore.

Last edited by dmail; 07-09-2006 at 11:59 AM.
 
Old 07-09-2006, 11:40 AM   #7
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by dmail
I know the reason, but for the life of me can't remember the work around. Whats happening is that you are reading from the standard input for the count and it leaves the newline '\n' on the standard input then when using getline it takes the newline off this is why you are not seeing anything.
I havn't got my books at hand at the minute, but I will have a little look around the net and post back.

[edit]
Didnt take long
after you call to read the count add
cin.ignore(10,'\n');
what this does is looks 10 characters in the standard input looking for a newline which it then disguards all the input upto and including the newline. NOTE: 10 is just a randon number I used.
what is the new code then???
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Two things: (Probably Simple) Grub question, and a gDesklets question Wilffo Linux - Software 3 05-20-2006 01:33 PM
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM
Simple C++ Question HappyDude Programming 3 10-26-2003 12:18 PM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 04:08 AM

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

All times are GMT -5. The time now is 07:00 AM.

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