Solved

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

Posted on 2008-06-23
14
278 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
  • 8
  • 6
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
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
Comment Utility
You could always go for a recursive function, but that's a bit overkill imo. Can you use recursive functions ?
0
 

Author Comment

by:windshire
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:windshire
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now