iam a noob at c++ and trying to learn it so go easy on me.

basically i am trying to enter a some letters such as 'helody' and then check the letters it has in it and place the remaining letters of the alpahebet after it.

so afterwords it would look like:
helodyabcfghij.....etc

here is what i have tried but it just crashes on me after i enter some characters.

can someone point out where iam going wrong please

**edit iam testing it with only a few letters thats why the counter is 5

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


int counter = 5;
char key [30];
char alpha [] = "abcdefghijklmnopqrstuvwxyz";

char nextchar;

int main()
{
	cin >> key;

for (int i=0; i<counter; i++)
{
 for (int f = 0; f<25; f++)
	{


	if (key[i] != alpha[f])
		{
		counter++;
		key[counter] = alpha[f];
		}
	}
}



return 0;
	
}

Recommended Answers

All 13 Replies

why are you using a character array? could easily do it with string and use concatenation.

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

int main()
{
	string input;
	cin>>input;
	string a="abcdefghijklmnopqrstuvwxyz";
	string output=input+a;
	cout<<output;
	return 0;
}

The OP wants to print the letters not used in the original "key" after they have output said key (I think the OP left an h in the extra letters of the example which was confusing).

I would use a string like lotrsimp said, but in your alpha collection write a number or other character over the used letters so if your key were "abd" your alpha string would look like "00c0efghijklmnopqrstuvwxyz"
A simple if statement gets you the chars you want.

The OP wants to print the letters not used in the original "key" after they have output said key (I think the OP left an h in the extra letters of the example which was confusing).

I would use a string like lotrsimp said, but in your alpha collection write a number or other character over the used letters so if your key were "abd" your alpha string would look like "00c0efghijklmnopqrstuvwxyz"
A simple if statement gets you the chars you want.

my mistake i did put an extra 'h' in.

many thanks i will try what you said and report back

edit**

i done what you said but how would i 'erase' the position of the 0's

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


int counter = 5;
string key;
string alpha = "abcdefghijklmnopqrstuvwxyz";

char nextchar;


int main()
{

	cout << alpha << endl;
cin >> key;

for (int i=0; i<key.length(); i++)
{
	for (int f = 0; f<alpha.length(); f++)
	{
		if (alpha[f] == key[i] )
		{
		
		 alpha[f] = '0';
		}
	}
}



cout << alpha << endl;

return 0;
}

One solution could be to replace the '0' by ' ' ie the space character. Is this allowed ?
Make a temp array and run a for loop over the alpha array and each time the character is not a (0 or ' ') copy it to the temp array.

Move each character after the '0' one location down the array.

One solution could be to replace the '0' by ' ' ie the space character. Is this allowed ?
Make a temp array and run a for loop over the alpha array and each time the character is not a (0 or ' ') copy it to the temp array.

i tried your method but i dont think i have done it correct as the program crashes

heres the part of the program that messes up:

int r = 0;
for (int t = 0; t<alpha.length(); t++)
{
if (alpha[t] != '0')
{
	output[r] = alpha[t];
	r++;
}
}

  cout << output << endl;

what am i doing wrong?

Where are you getting the crash ? I ran the same code and I got the correct output. This is my output

abcdefghijklmnopqrstuvwxyz
ajluz
0bcdefghi0k0mnopqrst0vwxy0
bcdefghikmnopqrstvwxy

Did you put a getch() after the last cout ?

There doesn't seem to be any requirement to keep an output array, so it may not be necessary to shift the arrays all around if you are just outputting the results to screen. Just say if alpha !='0' then output the character.

Where are you getting the crash ? I ran the same code and I got the correct output. This is my output

abcdefghijklmnopqrstuvwxyz
ajluz
0bcdefghi0k0mnopqrst0vwxy0
bcdefghikmnopqrstvwxy

Did you put a getch() after the last cout ?

no i diddnt put getch(). What does that do?

i tried putting it in anyway but it still crashes.

heres my code:

#include <iostream>
#include <cstdio>
#include <string>
#include <conio.h>
using namespace std;


int counter = 5;
string key;
string alpha = "abcdefghijklmnopqrstuvwxyz";
string output;
char nextchar;


int main()
{

	cout << alpha << endl;
cin >> key;

for (int i=0; i<key.length(); i++)
{
	for (int f = 0; f<alpha.length(); f++)
	{
		if (alpha[f] == key[i] )
		{
		
		 alpha[f] = '0';
		}
	}
}



cout << alpha << endl;

int r = 0;
for (int t = 0; t<alpha.length(); t++)
{
if (alpha[t] != '0')
{
	output[r] = alpha[t];
	r++;
}
}

  cout << output << endl;
getch();

return 0;
}

it seems to crash on the last part of the program doing the for loop as it prints evrything else out except the final cout line and just throws up crypt.exe stopped working

iam using visual c++ 6.0 on windows 7 64bit. so could it be compatability issues thats causing it to crash?

thanks for the reply jonsca but i forgot to mention that i need to do other things with the array afterwards. i was just outputting it in this instance for testing purposes

no i diddnt put getch(). What does that do?

Unfortunately getch is part of a nonstandard library for console input. A portable solution would be to put cin.get() in the same spot.

iam using visual c++ 6.0 on windows 7 64bit. so could it be compatability issues thats causing it to crash?

This is not outside the realm of possibilities. Try downloading the 2008 express edition as VC6 is almost 12 years old.

thanks for the reply jonsca but i forgot to mention that i need to do other things with the array afterwards. i was just outputting it in this instance for testing purposes

Gotcha. I'll try running your code and see what the outcome is.

the [] only work if string is defined

string has useful methods such replace()
find()

if you read up on the methods you will find cleaner solutions

but at first glance I think your error is here

output += alpha[t]

output[r] = alpha[t];

Google for getch()/ cin.get()
Put it before the last cout

the [] only work if string is defined

Yep, just came to the same conclusion.

I think what you meant Tetron was to substitute output +=alpha[r] for the output[r] = alpha[t] .

Another option would be to dynamically allocate a char array but that wouldn't be able to take into account repeats in the letters of key.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.