ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
#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;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.