I am new to C++ and programming in general. (Only 1 years experience with python and some perl)
I wrote a program that creates all possible words using the character set (char_set) = 'abcdefghijklmnopqrstuvwxyz' from a word length of 1 to a word length of 5.
x1 = 0
x2 = 0
It first takes char_set[x1] and sets that to the variable word. Then adds 1 to x1. Then repeats. Then once x1 == the length of char_set, it adds a new letter to the word. So word now equals char_set[x1]+char_set[x2] and repeats.
Code :
/*
* wlst.cpp
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <time.h>
using namespace std;
int main() {
time_t start, end;
start = time(&start);
int running = 1;
int max = 5;
int length = 1;
int x1=0,x2=0,x3=0,x4=0,x5=0,x6=0,x7=0,x8=0;
string chars = "abcdefghijklmnopqrstuvwxyz";
string word;
string blank;
while(running == 1) {
if(length == 1) {
word = blank+chars[x1];
cout << word << endl;
x1 ++;
if(x1 == chars.length()){
x1 = 0;
length ++; } }
else if(length == 2) {
word = blank+chars[x1]+chars[x2];
cout << word << endl;
x2 ++;
if(x2 == chars.length()){
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
x1 = 0;
x2 = 0;
length ++; } } }
else if(length == 3) {
word = blank+chars[x1]+chars[x2]+chars[x3];
cout << word << endl;
x3 ++;
if(x3 == chars.length()) {
x2 ++;
x3 = 0;
if(x2 == chars.length()) {
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
x1 = 0;
x2 = 0;
x3 = 0;
length ++; } } } }
else if(length == 4) {
word = blank+chars[x1]+chars[x2]+chars[x3]+chars[x4];
cout << word << endl;
x4 ++;
if(x4 == chars.length()) {
x3 ++;
x4 = 0;
if(x3 == chars.length()) {
x2 ++;
x3 = 0;
if(x2 == chars.length()) {
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
length ++; } } } } }
else if(length == 5) {
word = blank+chars[x1]+chars[x2]+chars[x3]+chars[x4]+chars[x5];
cout << word << endl;
x5 ++;
if(x5 == chars.length()) {
x4 ++;
x5 = 0;
if(x4 == chars.length()) {
x3 ++;
x4 = 0;
if(x3 == chars.length()) {
x2 ++;
x3 = 0;
if(x2 == chars.length()) {
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
x5 = 0;
length ++; } } } } } }
else if(length == 6) {
word = blank+chars[x1]+chars[x2]+chars[x3]+chars[x4]+chars[x5]+chars[x6];
cout << word << endl;
x6 ++;
if(x6 == chars.length()) {
x5 ++;
x6 = 0;
if(x5 == chars.length()) {
x4 ++;
x5 = 0;
if(x4 == chars.length()) {
x3 ++;
x4 = 0;
if(x3 == chars.length()) {
x2 ++;
x3 = 0;
if(x2 == chars.length()) {
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
x5 = 0;
x6 = 0;
length ++; } } } } } } }
else if(length == 7) {
word = blank+chars[x1]+chars[x2]+chars[x3]+chars[x4]+chars[x5]+chars[x6]+chars[x7];
cout << word << endl;
x7 ++;
if(x7 == chars.length()) {
x6 ++;
x7 = 0;
if(x6 == chars.length()) {
x5 ++;
x6 = 0;
if(x5 == chars.length()) {
x4 ++;
x5 = 0;
if(x4 == chars.length()) {
x3 ++;
x4 = 0;
if(x3 == chars.length()) {
x2 ++;
x3 = 0;
if(x2 == chars.length()) {
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
x5 = 0;
x6 = 0;
x7 = 0;
length ++; } } } } } } } }
else if(length == 8) {
word = blank+chars[x1]+chars[x2]+chars[x3]+chars[x4]+chars[x5]+chars[x6]+chars[x7]+chars[x8];
cout << word << endl;
x8 ++;
if(x8 == chars.length()) {
x7 ++;
x8 = 0;
if(x7 == chars.length()) {
x6 ++;
x7 = 0;
if(x6 == chars.length()) {
x5 ++;
x6 = 0;
if(x5 == chars.length()) {
x4 ++;
x5 = 0;
if(x4 == chars.length()) {
x3 ++;
x4 = 0;
if(x3 == chars.length()) {
x2 ++;
x3 = 0;
if(x2 == chars.length()) {
x1 ++;
x2 = 0;
if(x1 == chars.length()) {
length ++;
running = 0; } } } } } } } } }
if(length == max+1) {
running = 0;
return 0; }
}
end = time(&end);
float total_time = difftime(end, start);
printf("\n\nTOOK C++ A TOTAL OF: %.2f SECONDS\n", total_time);
}
Is this an efficient way to do this?
I don't get any errors but i want to know if i am doing something wrong.
Any help/suggestions/positive criticism is greatly appreciated.