Solved

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

Posted on 2008-06-23
14
288 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:windshire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 21846663
>> 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 ?
0
 

Author Comment

by:windshire
ID: 21846805
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21846998
You could always go for a recursive function, but that's a bit overkill imo. Can you use recursive functions ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:windshire
ID: 21847607
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

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 21847838
Just a quick note on that code (after quickly looking over it) :

>> if (answer='n')

the = operator is the assignment operator, not the comparison operator. The latter is ==, so you probably meant :

        if (answer == 'n')



No arrays, no strings, no recursive functions. That is extremely limiting for this kind of assignment. I would verify those restrictions with the teacher if I were you.
0
 

Author Comment

by:windshire
ID: 21848093
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
0
 

Author Comment

by:windshire
ID: 21848126
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
0
 

Author Comment

by:windshire
ID: 21848339
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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21848383
>> 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) ?
0
 

Author Comment

by:windshire
ID: 21849719
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

0
 

Author Comment

by:windshire
ID: 21849741
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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21849881
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 )
0
 

Author Comment

by:windshire
ID: 21850427
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

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21850771
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).
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question