having problem to find an error
-
Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:
#include
using namespace std;int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?
-
Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:
#include
using namespace std;int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?
for(int i=0;i<=l;i++)
Try changing to
for(int i=0;iYou're index is going out of bounds on the st variable 'st[i]', when i is equal to l.
Also, having nested for loops with the same index variable name is tough to read."the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle -
Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:
#include
using namespace std;int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?
After the following line
cin>>t;
there are still charactes in the input buffer, hence, the next call to getline gets them. A commont way to deal with this issue is using
cin.ignore
(see, for instance What is the use of cin.ignore() in C++?[^])."In testa che avete, Signor di Ceprano?" -- Rigoletto
-
Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:
#include
using namespace std;int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?
Have you tried stepping through the code using a debugger? Doing so will let you know in a hurry what is wrong.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:
#include
using namespace std;int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?
In addition to other answers. Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include
using namespace std;int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<
Indentation style - Wikipedia[^]Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein