Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi!
This program asks to
Read, from the keyboard, a sequence of le
tters (uppercase or lowercase) and save the
corresponding uppercase letter in
to a vector of characters (
vector<char>
) until the user enters the
character "." (period), which should not be inserted in
to the vector; characters other than letters or
characters that have already been typed previous
ly should not be inserted into the vector.
Present the resulting vector on the screen,
using the format shown in the example.

Example:
LETTER or "." ? r
LETTER or "." ? T
LETTER or "." ? 2
// invalid character; it not inserted into the vector
LETTER or "." ? t
// repeated letter; it not inserted into the vector
LETTER or "." ? P
LETTER or "." ? .
VECTOR: [ R T P ]

C++
#include <iostream>
#include <ctime>
#include <ctype.h>
#include <vector>

using namespace std;

char* UCASE(char s[256]){
   for(int i=0;s[i]!='\0';i++)
	{
		s[i]=toupper(s[i]);
	}
   return s;
}

int isletter(char s)
{
	int i;
	if (isalpha(s))
	    {i =1;}
	else
	    {i=0;}

	return i;
}



int main(){

	char letter[256];
	char ter;
	ter = atoi(letter);
	char end = '.';

	vector<char> v;


    do
    {cout << "LETTER OR '.' ? ";
    cin >> letter;
    char finletter;
    finletter = atoi(UCASE(letter));

    	if ( !(find(v.begin(), v.end(), finletter) !=v.end()) && isletter(ter)==1)
    {

    	v.push_back(finletter);
    };

    } while (ter!=end);

    {      cout << "VECTOR: [ ";

        	for (size_t n = 0; n < v.size(); n++)
        	    cout << v[ n ] << " ";
        	    cout << " ]" << endl;}


	return 0;
};



The output is asking correctly for the letters, but when putting '.' it continues to answer for a letter instead of displaying the vector.
I've tried just while(ter!='.') and then i did it with do while but it didn't resolve anything.
Can you help?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Apr-14 12:45pm    
Don't try; design your code. Use the debugger.
—SA

1 solution

You read the input into finletter, but the loop is comparing ter. ter is never set inside the loop.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Apr-14 13:00pm    
Good catch, a 5.
—SA
Nádia Carvalho 15-Apr-14 13:00pm    
I put ter= atoi(letter); inside the loop and it didn't resolve the problem.
Do you suggest another form of doing that?
NeverJustHere 15-Apr-14 13:05pm    
Look at the definition of atoi. Is that really what you want to be doing?

As Sergey said - run the code in the debugger, put in break points, you can examine the values of the variables throughout each step of the loop. This is a really useful way of finding errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900