Link to home
Start Free TrialLog in
Avatar of windshire
windshire

asked on

C++ how do I add chacters without using arrays and strings

Have a program have everything finsihed except one thing
i have to print letters without useing arrays or strings or 26 variables
can use for, while, do, if/else loops

has a starting letter
has a number that instructs it to find the next letter x number of spaces down

etc
letter='a';
number = 2;
new_letter=letter+number;
cout << letter << " " <<new_letter;

prints a c

word=letter+new_letter (just adds need something that will print and hold the actual letters)

asks is that a word if no
prints ac e

repeat until word is found or cycle back to the starting leter

where i am having the issue is printing the word or the previous letters
before the new_letter
ie
i can get it to print 2 letters
ac
ae
ag

but cant get it to print
ac
ac e
ace g

I dont want the code to make it work just some pointers on where I am going wrong. First semester in c++. thinking now taking a summer class in this might have been a bad move.
anyway any pointers will be very much appreciated

code so far
attached as code snippet

#include<iostream>
 
using std::cin;
using std::cout;
 
int main()
{
	char answer, letter, new_letter;
	char word=0;
	
	int number=0, count=0;
	
	
	cout << "\nWord Finder. User will enter a Letter then a number."
		 << "\nWord finder will move through the alphabit to the ."
		 << "\nnext letter by a count of the number you entered."
		 << "\nAsking User if the combination is a word or not.\n";
		 
	do{
		cout << "\nEnter a letter. ";
		cin >>  letter;
		letter=tolower(letter);
	  }while (!(letter >= 'a' && letter <= 'z'));		
	    cout << letter;
		
	do{
		cout << "\nEnter a number From 1 to 26.\n";
		cin >> number;
	}while (!(number >=0 && number <=26));
		cout << number;
		
	new_letter=letter+number;
	
	do{
		cout << "\nIS "<< letter << new_letter << " a word? y for yes. n for no ";
		cin >> answer;
		++count;
		letter = letter+new_letter;
		new_letter=new_letter+number;
		
		answer=tolower(answer);
    }while(answer != 'y' && count <10 && answer == 'n');
 
	if (count <10)
		cout << "\nFound a Word in "<< count<<" trys.";	
		
		else
			cout << "\nNo Words Found!!";
		
	return 0;
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

>> word=letter+new_letter (just adds need something that will print and hold the actual letters)

That's called a string :) Why can't you use it ?

Can you post the exact text of the assignment ?
Avatar of windshire
windshire

ASKER

we can only use what he has covered. and he has not covered string assignments. Yes this is what has me stumped. I am asking same question on his forum

i am capable of doing it with a string and array just cant figure out how to do it without using those and not making a ton of variables and if/else statments

/boggle but can only use what has been covered in class and strings have not been. Only other thing i can thin of is maybe he thinks he covered it.
You could always go for a recursive function, but that's a bit overkill imo. Can you use recursive functions ?
recursive has not been covered in class ;( so that is out as well

all these restrictions of only being able to use what was taught is driving me nuts

no array type for strings. heck if i could use a general array would store the number and convert it to a char but not even allowed to do that.
i am in a total brainpop mode
no strings other than  cout <<"blah blah"

just basic for, while, if loops/statments

i am trying to NOT have to do it the following manner
#include<iostream>
 
using std::cin;
using std::cout;
 
int main()
{
	char answer, letterOne=0, letterTwo=0, letterThree=0, letterFour=0, letterFive=0;
	
	int number=0, count=0;
	
	
	cout << "\nWord Finder. User will enter a Letter then a number."
		 << "\nWord finder will move through the alphabet to the ."
		 << "\nnext letter by a count of the number you entered."
		 << "\nAsking User if the combination is a word or not.\n";
		 
	do{
		cout << "\nEnter a letter. ";
		cin >>  letterOne;
		letterOne=tolower(letterOne);
	  }while (!(letterOne >= 'a' && letterOne <= 'z'));		
	    cout << letterOne;
		
	do{
		cout << "\nEnter a number From 1 to 26.\n";
		cin >> number;
	}while (!(number >=0 && number <=26));
		cout << number;
		
	letterTwo=letterOne+number;
	
	cout << "\nIS \""<< letterOne << " " << letterTwo << "\" a word? y for yes. n for no ";
	cin >> answer;
	answer=tolower(answer);
	{
		if (answer='n')
		{
			letterThree=letterTwo+number;
			cout << "\nIS \""<< letterOne << letterTwo << " " << letterThree <<"\" a word? y for yes. n for no ";
			cin >> answer;
			answer=tolower(answer);
		}	
	
			if (answer='n')
			{
				letterFour=letterThree+number;
				cout << "\nIS \""<< letterOne << letterTwo << letterThree << " " << letterFour <<"\" a word? y for yes. n for no ";
				cin >> answer;
				answer=tolower(answer);
			}	
			
			
					if (answer='n')
					{
						letterFive=letterFour+number;
						cout << "\nIS \""<< letterOne << letterTwo << letterThree << letterFour << " " << letterFive <<"\" a word? y for yes. n for no ";
						cin >> answer;
						answer=tolower(answer);
					}	
		else
				cout << "\nYou found a word";
	}		
		
		
   
 
	if (count <10)
		cout << "\nFound a Word in "<< count<<" trys.";	
		
		else
			cout << "\nNo Words Found!!";
		
	return 0;
}	

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i am trying to verify. no luck so far.
unfortunatly im one of those people that refuse to stop something until i find a solution or a solution base.

very bad trait when you have other things that need to be addressed. cant stop thinking how to fix this ddarn thing

oh, /im a dorknob --thanks for the operator correction
ya i am trying like heck to not use the multitude of if/else and 26 variables one for each possible combinations (

i just feel like he is not looking for that. Though I am going to do one just for shiets and giggles so ill have one working 100% properly though poorly coded
ok all those resrtictions were correct. but got a pointer from instructor on how to do it within boundries

i feel like an idiot now

It's a good question.

Print the same variable over and over again, after updating it each time.

For example:

char letter = 'a';
while ( letter <= 'z' )
{
     cout << letter;
     letter += 1;
}

will print the letters 'a' through 'z'

cant believe i could not figure this out. oh well sometimes something simple bites one in the arse

will award points for your quick responce and efforts
>> will print the letters 'a' through 'z'

Yes, but I thought you needed to keep the state of the previous letters, and ask the user's confirmation for each letter ? In that case, this won't work.


Can you post the exact text of the assignment (as I asked earlier) ?
ok here is how i have that to work

letter = the original letter inputed
number= orignal number input for how many letters over from the original letter it will move
new_letter = letter + number
the below code is placed inside a for loop adding ++cycle everytime the combined letters is not a word

so the while loop will output one additional letter at an interval of number

so if cycle was set a 1 (from the ++cycle in the for loop this is inside of) and letter was 'a' and number was 2

will print
a

cycle set at 2
will print
ac

cycle set to 3
ace

etc
so i was on the mindset i needed to store the letter but what ended up needing is to have it go through adding another character each time we said it was not a word
	while (new_letter <= 122 && count <= cycle )//122 is ascii z
	{
		cout << letter ;
		letter+=number;
		++count;
	}

Open in new window

sorry in advance am new to c++, 2 weeks, so new i dont even know how to properly ask a question about it

Thanks a ton for your help
No problem. I apparently completely misunderstood what you wanted to do :) So you don't really need the user's confirmation then, like :

        cout << "\nIS "<< letter << new_letter << " a word? y for yes. n for no ";


btw :

>>         while (new_letter <= 122 && count <= cycle )//122 is ascii z

Why not make things a bit more clear, and use 'z' instead of 122 ?

        while (new_letter <= 'z' && count <= cycle )
still need to output to look like
Is  "ac e" a word? y for yes, n for no

if the number + letter is greater than 'z' (122) need it to not print any other character but lowercase letters

so if letter 'a' (97) and number = 10
the 3rd cycle through it will = 127 and print the extended ascii character
 with useing the number i can simply  new_letter > 122  new_letter = new_letter-26 to start it over from the begining. will print the letter long as i keep it a char

not sure if this is the best way to do it but im new and dont really know how to code effeciently

Take a look at the modulo operator (%). It returns the remainder after division. More specifically value % 26 will always be a value between 0 and 25 (inclusive).