0

In my code,i need to use nested while loop and it is not working correctly.It is skiping the 1st iteration of the nested loop[This is my guess].For example,


#include<iostream>
#include<string>
using namespace std;

void solve()
{
    char s[20];
    char c;
    int i=0;
    while(true)
    {
        scanf("%c",&c);
        if(c=='\n')
        {
            break;
        }
        if(c!=' ')
        {
            s[i]=c;
            i++;
        }
    }
cout<<s<<endl;

}

int main()
{

    int t;
    cin>>t;

    while(t--)
    {
        solve();
    }
    return 0;
}

In this above code,when i give the below input,i dont get accurate result. enter image description here

It should have take 3 string input.But it terminates the program after taking 2 string input. What i want from the code is, if the user input of t=3,then i need to take 3 string input in char datatype array using loop. But my code is not giving the desired result. What is the problem of this code?

POKA
  • 29
  • 6
  • 3
    I see three issues: 1) You use unformatted input after formatted input which explains the skipped line. See the same issue disussed [here](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). 2) You don't test that the `scanf` actually succeeds. 3) You don't put a terminating `\0` in `s` so you'll have undefined behavior. – Ted Lyngmo Jan 21 '21 at 12:56
  • 1
    The input buffer contains `"3\n..."`. Then you read an integer, leaving `"\n..."` in the buffer. Then you start calling `solve`. – molbdnilo Jan 21 '21 at 12:59
  • @TedLyngmo can you please give me some details and explainations.Like how to test scanf? and how can i put /0 in s? and why? – POKA Jan 21 '21 at 13:01
  • Sure, the explanation for the skipped line is in the link. `scanf` returns a value corresponding to the number of matched fields. In your case, you should test that it returns `1`. A `char`acter string needs a terminating `\0` character to let the printing function know where the string ends - or else it will continue reading from memory that doesn't belong to the program or read memory in the program it shouldn't read from. Anyway, when you do that, anything can happen, which is what [_undefined behavior_](https://en.cppreference.com/w/cpp/language/ub) (or UB for short) means. – Ted Lyngmo Jan 21 '21 at 13:04
  • A fourth problem is that you don't test if `i` steps out of the boundaries of your array `s`. If you do `s[i]=c;` when `i` is 20 or more, you also have undefined behavior. – Ted Lyngmo Jan 21 '21 at 13:08
  • @TedLyngmo well i made some changes,Like while(i<20) and s[0]='/0'; and in the condition, i modifed it to if(c=='\n' || c=='\0').Still having the same error – POKA Jan 21 '21 at 13:18
  • @POKA Did you read the link which explains the perils of mixing formatted input with unformatted input? Also, `while(i<20)` doesn't leave room for `s[i] = '\0';` after the `while` loop. The `\0` must be last, not first. `while(i < 20 - 1) { ... } s[i] = '\0';` would be better. – Ted Lyngmo Jan 21 '21 at 13:25

1 Answers1

1

As you are using c++ why not you use string data type and make it simpler, like:

#include<iostream>
#include<string>
using namespace std;

void solve()
{
    //char s[20];
    string s;
    
    cin >> s;
    cout << s << "\n";
}

int main()
{

    int t;
    cin>>t;

    while(t--)
    {
        solve();
    }
    return 0;
}

Sahadat Hossain
  • 2,470
  • 2
  • 6
  • 13
  • no i cant,cause i need to iterate through every charater of the string.And i dont want to use additional loops. – POKA Jan 21 '21 at 13:08
  • here in your code you are using loop during taking the input, so if you take the input like i mentioned and use a loop to traverse the characters of the strings then it won't add any additional complexity, it will be same as yours and additionally it will be easier than yours. And also if you use `char s[20]` then you cannot store of the string size is 20 or more, so i hope you got it. – Sahadat Hossain Jan 21 '21 at 13:23