problem in the for loop :(
May 18, 2013 at 6:33pm UTC
actually i am trying to break down a string of integers into an array of strings of 4 characters....but i am having problem in the loop that is why i can't do it.
"strs is a string array of size "next_line_count"which i declare and it is working fine."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
vector <string> myarray;
string string_of_4_nos;
char string_storage;
string string_of_4;
for (int j=0;j<next_line_count;j++)
{
for (int k=0;k<strs[j].size();k++)
{
for (int l=0;l<4;l++)
{
string_storage=strs[j].at(k);
k++;
string_of_4+=string_storage;
}
myarray.push_back(string_of_4);
}
}
for (int j=0;j<myarray.size();j++)
cout<<myarray[j]<<endl;
actually i am having problem in
1 2
string_storage=strs[j].at(k);
statement.
Last edited on May 18, 2013 at 6:33pm UTC
May 18, 2013 at 6:43pm UTC
Giving the full code would help.
May 18, 2013 at 6:48pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
// project type 2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
ifstream myfile;
string file_data;
//vector <string> str;
string *strs;
int next_line_count=0;
int i=0; //used in string array indexing
myfile.open("lets have fun.txt" );
//if(myfile.is_open())
{
char a;
while (!myfile.eof())
{
next_line_count++;
myfile.get(a);
while (a!='\n' )
{
next_line_count++;
myfile.get(a);
}//cout<<"test1";
}//cout<<"test2";
}//cout<<"test3";
strs=new string[next_line_count];
myfile.close();
myfile.open("lets have fun.txt" );
if (myfile.is_open())
{
while (myfile.good())
{
getline(myfile,file_data);
//cout<<file_data;
strs[i]=file_data;
//cout<<strs[i];
i++;
}
//strs=new string [next_line_count];
}
for (int j=0;j<next_line_count;j++)
cout<<strs[j]<<endl;
//***********************************************************************************************
//dividing a string into chunks
vector <string> myarray;
string string_of_4_nos;
//string myarr [100];
char string_storage;
string string_of_4;
//int strarr=0;
for (int j=0;j<next_line_count;j++)
{
for (int k=0;k<strs[j].size();k++)
{
for (int l=0;l<4;l++)
{
string_storage=strs[j].at(k);
//k++;
//string_of_4+=string_storage;
}
myarray.push_back(string_of_4);
}
}
for (int j=0;j<myarray.size();j++)
cout<<myarray[j]<<endl;
system("pause " );
return 0;
}
Topic archived. No new replies allowed.