Newbie here,
I am using getline in order to take in a sentence with spaces in it. Typing in a sentence with no spaces does not cause a Segmentation fault (core dumped) error. Oddly enough, typing in, "Z L" does not cause the error either, but for example, "Z LL","Z LLLL" "ZZZ LL" all do (random characters).
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char a1[20];
cout<<"Please type in a grammatically correct sentence of 3 words using no punctuation ";
cin.getline (a1, 20);
string a = a1;
cout<<endl<< a <<endl;
int q2=a.length();//Cause of Error?
cout<< "test 1"; // the error appears before this
int q1=0;
for(int q=0;q<q2;q++)
{ cout<<"test";
if(a[q]==' ')
{cout<<q1;
q1++;}
}
if(q1<1)
{cout<<"Ungrammatical, less than 3 words"<<endl;
return(0);}
This issue appeared as soon as getline was first used. I've been searching for the past day for a solution, and I've unsuccessfully attempted cin.ignore() and the like. I've added the int q2 line in order to try and pinpoint the cause, and I understand that it is redundant and can be skipped and just directly used in the for loop.
The 'best results' I've obtained is with the code I posted- I started off first not using char, but I would always get the error, regardless of what was inputted.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char a1[20];
cout<<"Please type in a grammatically correct sentence of 3 words using no punctuation ";
cin.getline (a1, 20);
string a = a1;
int q1=0; // counter
for(int q=0;q<a.length();q++)
{
if(a[q]==' ')
{q1++;}
}
if(q1<1)
{cout<<"Ungrammatical, less than 3 words"<<endl;}
else
{cout << "spaces = "<<q1;}
return(0);
}
@SamuelAdams
the code goes beyond what i pasted- but I want it to exit if its ungrammatical, thus the return(0) before the }. Does the return cause this error? Should I remove it fully?
Why are you using the C-string? Why not just use a std::string in the first place with the proper getline() function? There are two getline() functions one that works with the C-string, the one you're using, and one that works with the std::string.
@jlb thank you for your quick reply-
Its odd that it works for you but stops at a certain point where I'd assume I would find the source of the error. I just went back and removed bits and bits of code until i could run the program without crashing and found this to be the final bit where I would still crash. Is this situation (where code crashes at a certain point due to code that wasn't even "visibly" reached?) common? Regardless,
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char a1[20];
cout << "Please type in a grammatically correct sentence of 3 words using no punctuation ";
cin.getline (a1, 20);
string a = a1;
cout << endl << a << endl;
int q2 = a.length();//Cause of Error?
cout << "test 1"; // the error appears before this
int q1 = 0;
for(int q = 0; q < q2; q++)
{
cout << "test";
if(a[q] == ' ')
{
cout << q1;
q1++;
}
}
if(q1 < 1)
{
cout << "Ungrammatical, less than 3 words" << endl;
return(0);
}
string word1= " "; //the words the user enters are attempted to be split
string word2= " ";
string word3= " ";
bool one= true;
bool two= false;
bool three= false;
for(int i=0;i<a.length();i++)
{
if (a[i] !=' 'and one==true)
{string word1 = word1 + a[i];}
if (a[i] == ' 'and one==true)
{bool one=false;
bool two=true;}
if (a[i] != ' 'and two==true)
{string word2 = word2 + a[i];}
if (a[i] == ' ' and two==true)
{bool two=false;
bool three=true;}
if (a[i] != ' 'and three==true)
{string word3 = word3 + a[i];}
if (a[i] == ' 'and one==true)
{bool three=false;}
}
cout<<word1<<" "<<word2<<" "<< word3<<endl;
}
I understand this code might have many more mistakes in it- I would appreciate only receiving feedback on the error i started this topic for if possible. If it is all tied in, sure, but I would prefer learning from my own mistakes and not have this code being fully written out!
Again, thank you for your replies and help
Notwithstanding the blooper in not taking the advice of jlb, an experienced programmer, "and" is not legal in C++. && is! But only fix it up if you want to. :)
@SamuelAdams,
wow, it works!
Thank you so much your help, and everyone else's.
Could you also explain, if you know, why this would lead to an error as this?
Cheers!
@kemort,
I didn't mean any offense in not changing my style and taking other advice, as I was focused on fixing this error that's been driving me nuts for the past day. I'll get on fixing everything else next!
That link you provided appears to be a PHP link not C++.
From the C++ standard:
2.6 Alternative tokens [lex.digraph]
1. Alternative token representations are provided for some operators and punctuators. (16)
2. In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling.(17) The set of alternative tokens is defined in Table 2.
(16) These include “digraphs” and additional reserved words. The term “digraph” (token consisting of two characters) is not perfectly descriptive, since one of the alternative preprocessing-tokens is %:%: and of course several primary tokens contain two characters. Nonetheless, those alternative tokens that aren’t lexical keywords are colloquially known as “digraphs”.
(17) Thus the “stringized” values (16.3.2) of [ and <: will be different, maintaining the source spelling, but the tokens can otherwise be freely interchanged.