Home > Software engineering >  How to use the result of a loop in c
How to use the result of a loop in c

Time:03-18

I am writing a code to split a string into substrings. The idea I got was using the do... while loop but the code can only print the result.

What I need is how to assign the result of loop into a variable so that I can use the result after the loop has ended.

This is the code

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
    string s = "Somewhere down the road";
    string arrDel;
    istringstream iss(s);
    do {
        string subs;
        iss >> subs;
        string arrDel= '"'   subs   '"'   ", ";
        cout << arrDel;
    } while (iss);
}

CodePudding user response:

You only put the type of a variable before the name the first time. When reassigning to it, you just use the name. So declare your variable as

string arrDel;

and then, inside the loop, assign with

// Note: We don't say "string" here
arrDel = ...;

CodePudding user response:

You have created a second variable that 'shadows' the one you already created

int main()
{
  string s = "Somewhere down the road";
  string arrDel; <<<<<<<<<===========
  istringstream iss(s);
  do
  {
   string subs;
   iss >> subs;
   string arrDel= '"'   subs   '"'   ", "; <<<==creates a new variable called arrDel
   cout << arrDel;
 } while (iss);
}

you need

int main()
{
  string s = "Somewhere down the road";
  string arrDel; 
  istringstream iss(s);
  do
  {
   string subs;
   iss >> subs;
   arrDel= '"'   subs   '"'   ", "; 
   cout << arrDel;
   } while (iss);
  }

CodePudding user response:

In each iteration of the loop you are declaring a new string arrDel.

string arrDel= '"'   subs   '"'   ", ";

though you already declared the string before the loop

string arrDel;

Also instead of the do-while loop you should use a while loop or for loop. That is you need to check the state of the stream to determine whether extracting of a substring was successful.

It seems what you are trying to do is something like the following

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string s = "Somewhere down the road";

    std::string arrDel;
    std::istringstream iss( s );

    for (std::string subs; iss >> subs; )
    {
        if (!arrDel.empty()) arrDel  = ", ";

        arrDel  = '"'   subs   '"';
    }

    std::cout << arrDel << '\n';
}

The program output is

"Somewhere", "down", "the", "road"

Or if you need a set of substrings that will be used later then you can use the standard container std::vector<std::string> as for example

#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>

int main()
{
    std::string s = "Somewhere down the road";

    std::vector<std::string> v;
    std::istringstream iss( s );

    for (std::string subs; iss >> subs; )
    {
        v.push_back( subs );
    }

    for (size_t i = 0; i < v.size(); i  )
    {
        if (i) std::cout << ", ";
        std::cout << std::quoted( v[i] );
    }
    std::cout << '\n';
}

The program output is the same as shown above that is

"Somewhere", "down", "the", "road"
  • Related