So here is the question and it is really stumping me because I can print the alphabet once but cant figure out how to repeat it.

First create a character array that contains the 26 letters as shown here. //Easy I can copy and paste ha.

char cAlphabet[] = {'a','b','c','d','e','f',
		'g','h','i','j','k','l','m','n','o','p',
		'q','r','s','t','u','v','w','x','y','z'};

Then create a vector of strings where the first string is 'a' and the second is 'bc', the third, 'def', etc. Create the number of strings requested by the user. Once you reach the letter z, start again with first letter in the alphabet. When you are done, print each vector to the screen.

How many strings would you like to create?
8

a
bc
def
ghij
klmno
pqrstu
vwxyzab
cdefghij

Here is what I have so far it works up until 6 or 7 strings but I dont know how to fix it so that it will continue to print strings with the alphabet starting over. Anything helps, Thanks.

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


void printVector( vector<string> v )
{
  for(unsigned int i = 0; i < v.size(); i++)
  {
    cout << v[i] << "\t";
  }
  cout << endl;

	return;
}

int main()
{
int nStrings=0;

string str1=" ";

vector<string> myVector;

char cAlphabet[] = {'a','b','c','d','e','f',
		'g','h','i','j','k','l','m','n','o','p',
		'q','r','s','t','u','v','w','x','y','z'};

str1=cAlphabet;
string str=" ";
str= str1.substr(0,26);
cout<<str<<endl;

cout<<"How many strings would you like?"<<endl;
cin>>nStrings;

for(int i=0; i < nStrings; i++)
{
	static int j=0;
	j= j + i;

myVector.push_back(str.substr( j, (i+1) ));

printVector(myVector);
myVector.clear();

}


return 1;
}

It seems like you may want to consider collecting the characters themselves into a vector, then extract the contents of the vector into a string, then pushback the string into your vector of strings.

It would cause additional overhead but you wouldn't have to worry about the string classes limitation on substring.

I'm also curious about the following--

for(int i=0; i < nStrings; i++)
{
	static int j=0;
	j= j + i;

myVector.push_back(str.substr( j, (i+1) ));

printVector(myVector);
myVector.clear();

}

-- why not j%26 for first character selection in the event that j+i > your array length - 1?

That's only half the battle. Eventually with nStrings at a high value you'll have to cycle through the array many more times than just one to concatenate appropriate characters.

I would strongly recommend using a second for loop to collect character together then store them in the vector of strings.

simple colution

int main()
{
    int num;
    char chars[] = "abcdefghijklmnopqrstuvwxyz";
    cout << "How many strings do you want to create?\n";
    cin >> num;
    long n = 0;
    for(int i = 1; i < num+1; i++)
    {
        for(int j = 0; j < i; j++, n++)
            cout << chars[n%26];
        cout << '\n';
    }
}

Edited 7 Years Ago by Ancient Dragon: n/a

Ok so took Ancient Dragons code and modified it to use vectors and strings like in my question but i cant seem to populate my vector with either the char or the string. I think it would be easier to use the char but I have no clue where to start for that. Here is my code using strings but it is giving me an error that "error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'char' to 'const std::string &'" in the line in my for loop. Ive tried all sorts of ways to populate it and I just know theres some easy way im looking passed.

#include <iostream>
#include <string>
#include <vector>

using namespace std;
   
void printVector( vector<string> v )
{
  for(unsigned int i = 0; i < v.size(); i++)
  {
    cout << v[i] << "\t";
  }
  cout << endl;

	return;
}


int main()
{
   int num;   
   string str1=" ";
   char chars[] = "abcdefghijklmnopqrstuvwxyz";
   str1= chars;
string str=" ";
str= str1.substr(0,26);

vector<string> myVector;

for(int i=0; i < str.length() ; i++)
{
myVector.push_back(str[i]);
}

printVector(myVector);

   cout << "How many strings do you want to create?\n";
   cin >> num;
   
   long n = 0;
   
   for(int i = 1; i < num+1; i++)
    {
  
      for(int j = 0; j < i; j++, n++)
		{
			
	  cout<<myVector[n%26];
		}
	  cout<<"\n";
     }
  
   return 0;
      }

lines 22 and 25: delete the = " " There is no value to initializing the strings with a single space.

line 25: delete that line because it has no value.

line 48: useless. What you want is to build a std::string object, then after the loop finishes insert it into the vector

   for(int i = 1; i < num+1; i++)
    {
      str1 = ""; // clear the string for new line 
      for(int j = 0; j < i; j++, n++)
        {
        str1 += chars[n%26];
        }
      myVector.push_back(str1);
     }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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


void printVector( vector<string> v )
{

  for(unsigned int i = 0; i < v.size(); i++)
  {
    cout << v[i];
  }
  cout << endl;

	return;
}


int main()
{
    int nStrings=0;
    
    string str="";
    
    vector<string> myVector;
    
    char cAlphabet[] = {'a','b','c','d','e','f',
    		'g','h','i','j','k','l','m','n','o','p',
    		'q','r','s','t','u','v','w','x','y','z'};
    
    str=cAlphabet;
    cout<<str<<endl;
    
    cout<<"How many strings would you like?"<<endl;
    cin>>nStrings;

    int dist = 0;
    for(int i=0; i < nStrings; i++)
    {
    	static int j=0;
        // the %26 keeps j (the starting point of the substring)
        // within the bounds of the alpha bet
    	j = (j + i)%26;
        

        dist = i+1;
        // this checks to see if the distance from the starting point
        // overflows the alphabet limit
        // if it does it sets it so that the distance from j will hit
        // 25 so that the later overflow check can start from 0
        // which makes the loop from z being 25 and a being 0
        if(j + (i+1) > 26){
            dist = 26 - j;      
        }
        
        myVector.push_back(str.substr( j, dist ));

        // again checking for overflow but this time using the % operator on
        // the overflow to see how far it is going over
        // then just starting from 0 pushback that far.
        if(j + (i+1) > 26){
          int overflow = (j + (i+1))%26;

        // because the number of letters displayed is always going to be
        // (i+1) this while loop makes sure that for the cases when dist + overflow don't
        // add up to (i+1) another alphabet is pushed onto the vector
        // this is because the overflow uses % and if the value is greater than 26 the % will
        // cut out that entire alphabet that should be inserted before the overflow is added
            while((i+1) - (dist + overflow) != 0){
                myVector.push_back(str);
                dist+=26;
            }

                myVector.push_back(str.substr(0, overflow ));
        }
        printVector(myVector);

        myVector.clear();
    
    }

    system("pause");
    return 1;
}

I modified this using your original post hope the comments help you understand how it works. Just ask any questions if you have any brah.

Edited 7 Years Ago by ScottieF: n/a

How would you get it to print a vector with an inputted amount of characters with no incomplete strings like this:

If I wanted five characters per string, it would print:

"abcde fghij klmno pqrst uvwxy"

How would you get it to print a vector with an inputted amount of characters with no incomplete strings like this:

If I wanted five characters per string, it would print:

"abcde fghij klmno pqrst uvwxy"

hey are you working on the last review test? me too.... this is what i have so far, i dont know how to get it to stop if the string is not complete, and this is probably not the best way to do it but i think it works..........

#include <iostream>
#include <vector>
#include <string>
using namespace std;  
void printVector( vector<string> v )
{   for(unsigned int i = 0; i < v.size(); i++)  
{    cout << v[i];  }  
cout << endl; 
return;} 
int i=0;
int main()
{    
	int nStrings=0;    
	string str="";    
string str2, str3,str4,str5;
	vector<string> myVector;   
	char cAlphabet[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};   
	str=cAlphabet;    
	cout<<str<<endl;    
	cout<<"How many characters per string would you like?"<<endl;    
	cin>>nStrings;   
	int dist = 0;   
	str2=str.substr(0,nStrings);
	str3=str.substr(nStrings,nStrings);
	str4=str.substr(nStrings*2,nStrings);
	str5=str.substr(nStrings*3,nStrings);
	
		cout << str2 << " " << str3<< " " << str4 << " " << str5 <<endl;
myVector.clear();        
				 system("pause");    
		return 1;}

hopefully this helps....... if you figure out how to get with no imcomplete strings could you reply back? im working on the same problem as well, thanks.

Edited 7 Years Ago by Nick Evan: Added code-tags

I just fixed my code and its working. I'll give it to you if you promise not to turn my exact code in. I'll be at office hours today so if the proctor tells me someone turned this in already... I'll find you. ;). Yeah I got marked for correct two times. Once because I told to proctor I wasn't going to finish during office hours and again last night when I didn't have the correct code. As of now I do, I think. It's working from what I can see. Here ya go. Btw, did you have the correct employee code? Mine compiles but I am too scared to setup my main function to show it works. I am sure its buggy. This class is annoying...

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


void printVector( vector<string> v )
{

  for(unsigned int i = 0; i < v.size(); i++)
  {
    cout << v[i] << " ";
  }


	return;
}


int main()
{
    int nStrings=0;
	int j = 0;
    
    string str="";
    
    vector<string> myVector;
    
    char cAlphabet[] = {'a','b','c','d','e','f',
    		'g','h','i','j','k','l','m','n','o','p',
    		'q','r','s','t','u','v','w','x','y','z'};
    
	for(int x = 0; x < 26; x++)
	{
    str = str+cAlphabet[x];
	}

	cout << str << endl;
    
    cout<<"How many strings would you like?"<<endl;
    cin>>nStrings;

	int limit = 26/nStrings;

    for(int i=0; i < limit; i++)
    {

        
        myVector.push_back(str.substr(j*nStrings , nStrings));
    
		printVector(myVector);
		
		myVector.clear();

		j++;

    }

	cout << endl;


    return 1;
}

Edited 7 Years Ago by evilctofwoc: n/a

thank you! i promise i'll change it up a little bit......here is what i have so far for Employee, it compiles but is not even close to being done, not sure how much this will actually help you. Im having trouble defining the string in the class and not sure how to call it in the main function, it keeps getting errors.....

class Employee
{
private:
	
	
  float earned;
  float HourlyRate;
 float rate;
  unsigned int ID;
  float nDays;
  
public:
void get_values(float,float);
 

	float Pay(){return(nDays*HourlyRate*8);}
	float Salary(){return(Employee::Pay());}



};


#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;

void Employee::get_values(float a, float b){
    HourlyRate=a;
    
    nDays=b;

}


float HourlyRate=0;
string name;
float nDays=0;
int i=0;

int main ()
{
    Employee chart;
	
    cout << " enter hourly rate" << endl;
    cin >> HourlyRate;
    
    cout << "enter # of days" <<endl;
    cin >> nDays;
    chart.get_values(HourlyRate,nDays);
    
    cout <<"Pay recieved is " << chart.Pay()<< " dollars" <<endl;
    
    return 1;
}

I got mine working. It turns out I had most of it right. I had to make a few minor adjustments but it's working. If I pass the test w/ the corrections I made, I'll send you a copy of the code. I want to wait until I finish because I don't want a lot of my code to be floating around on here, after-all all I had to do was google the question and this thread came up. I am sure others are doing the same. Good luck!

Btw, have you coded before? Your code looks like you take a lot of short cuts and I can't really understand them. What I would do is create seperate methods for each get/set.

Edited 7 Years Ago by evilctofwoc: n/a

This article has been dead for over six months. Start a new discussion instead.