Greetings all!~ I'm a newb when it comes to C++ and could use a bit of help with this code. It is supposed to take any character input, store it in array and then if there are lower case letters make them upper case. After if goes through the first run, it is supposed to again prompt for another set of characters. If Q is entered the program exits. As far as I can tell it runs fine until Q is pressed. It prints Q then again prompts for an input. I've been working on this for going on 18hours straight so any help would be greatly appreciated. And before I'm told I can use library functions as toupper and such, the assignment is to do it with math operators, and compare every letter for lowercase against ascii before moving onto the next :)

Thanks again guys (and ladies)

#include <string>
#include <iostream>


using namespace std;



const int MAX = 11;
int getInput();
char arrayInput[MAX];
void cStringToUpper (char[MAX]);
const char SENTINEL[MAX] = "Q";
char arrayOutput[MAX];

int main()
{
	

	getInput();

	return 0;

}

int getInput()
{
	

	cout << "\n" << "Get Input: ";
	
	cin >> arrayInput;
	
	while (arrayInput != SENTINEL)
	{
			cStringToUpper(arrayInput);
	
	}
	cout << "Goodbye!~";
	exit(0);
	
	cin.ignore(2);
	
return 0;
}


void cStringToUpper (char arrayInput[])
{
	char arrayOutput[MAX]; 
	int size=0;
	int j = 0;
	
	while (arrayInput[size] != '\0')
	{ size++; }

	while (arrayInput[j] != '\0')
	{
	if ((arrayInput[j] >= 97) && (arrayInput[j] <= 122))
	{
		//if ((arrayInput[j] >= 97) && (arrayInput[j] <= 122))
	//{
		arrayOutput[j] = arrayInput[j]-32;
		}
	else
	{
		arrayOutput[j] = arrayInput[j];
	}
	j++;
	}
	
	
for (int i = 0; i<size; i++)
	{
		cout << arrayOutput[i];
	}
	
	getInput();

	
}

Recommended Answers

All 11 Replies

1) Why are you entering a single input and calling the convert function on that single input?
2) In the convert function, why are you testing the array just to find the size, then going through an identical loop structure to test/convert each character? Use 1 loop.
3) In the convert function, why are you outputting each character in a loop? If the array is a c-string why not just output the string?
4) In the convert function, why are you recursively calling the input function? Wouldn't it be better to:

Start Loop
    Get Input
    Convert Input
    Output Converted String
End Loop

Let the getinput() function get the input, not do all the work.

Ok,

I am not just entering one character at a time. I'm entering a string of 8-11 characters into the array. I am determining the size of the array to use later down the so I can count to it when outputting the array. I had a problem that if I entered a string like "four" It would print out FOUR then a bunch of garbage behind it. That seemed to fix that.
To Be Honest, If i knew how to "Test each letter individually" to see if it was lowercase, and if it was, change it to upper case useing a string I would, however I'm barely grasping these concepts so I'm working with what I can.
I am recursively calling the input function to loop the program back to the input. I appreciate the suggestions and mull them over and see if I can rearrange my code to make it work.

Ok I cleaned up the code up some per your suggestions (I think). At least I made most of it all one loop. I tried to // the getInput(); call in the convert function and it continuously repeated so I left it in.

int main()
{
	getInput();
	while (arrayInput != SENTINEL)
		{
			cStringToUpper(arrayInput);
		}

	return 0;
}

int getInput()
{
	cout << "\n" << "Get Input: ";
	
	cin >> arrayInput;
	
return 0;
}

int cStringToUpper (char arrayInput[])
{
	char arrayOutput[MAX]; 
	int size=0;
		
	while (arrayInput[size] != '\0')
	{
	if ((arrayInput[size] >= 97) && (arrayInput[size] <= 122))
	{
		arrayOutput[size] = arrayInput[size]-32;
		}
	else
	{
		arrayOutput[size] = arrayInput[size];
	}
	cout << arrayOutput[size];
	 size++; 
	}
	getInput();
	return 0;
}

I am not just entering one character at a time. I'm entering a string of 8-11 characters into the array.

I never said anything about 1 character. I said 1 input. Your input is a c-string.

I am determining the size of the array to use later down the so I can count to it when outputting the array. I had a problem that if I entered a string like "four" It would print out FOUR then a bunch of garbage behind it. That seemed to fix that.

But if your string ends in '\0', outputting with cout will stop at the end of the string. Therefore, something else is wrong and you are probably changing the '\0' to something else. Go through your loop carefully to see if it ends properly.

To Be Honest, If i knew how to "Test each letter individually" to see if it was lowercase, and if it was, change it to upper case useing a string I would, however I'm barely grasping these concepts so I'm working with what I can.

Ahh, but you are testing each letter individually. That's exactly what your loop does.

I am recursively calling the input function to loop the program back to the input. I appreciate the suggestions and mull them over and see if I can rearrange my code to make it work.

Very bad programming to recurse just to start over. Starting over is best done with a loop.

Ok I cleaned up the code up some per your suggestions (I think). At least I made most of it all one loop.

Well, actually you did most of suggestion 2, and none of suggestion 3 & 4. So maybe about 30% of my suggestions were actually used. Think more on my suggestions (take the time needed to understand them) and try again. I actually gave you a BIG clue how to fix the program.

Well now I know my loop isn't working correctly. Now I need to understand why it isn't.

Well first off, thanks for your time helping me out. Ok lets take another stab at this. I reorganized the program, however I still can't get the sentinel to work properly but everything else seems to be working properly. I can see where before it was poorly organized. I hope this is an improvement and I think that I completed suggestions 3 & 4. Any suggestions are gladly appreciated.

#include <string>
#include <iostream>

using namespace std;

const int MAX = 11;
char arrayInput[MAX];
void cStringToUpper (char[]); 
const char SENTINEL[2] = "Q";
char arrayOutput[MAX];

int main()
{
		
	do{
		cout << "\n" << "Get Input: ";
		cin >> arrayInput;

		cStringToUpper(arrayInput); //Call Convert Funtion
		cout << arrayInput;			//Print Converted Char Array
	
	}while (arrayInput != SENTINEL);
	
	cin.ignore(2);
	return 0;
	}



void cStringToUpper (char arrayInput[])  
{
		int size=0;
	
		
		while (arrayInput[size] != '\0') //while the array does not equal the null												term, carry out the convert function
		{
	if (arrayInput[size] >= 97 && arrayInput[size] <= 122) //Determine if input																	letters are lower case
		
		arrayInput[size] = (arrayInput[size])-32; //Capitalize Lower Case Letters
		
	else
	
		arrayInput[size] = arrayInput[size]; //Print all other letters as they were													entered
	
	size++; 
		}
}

Your formatting makes the code difficult to follow. Here's a writeup about formatting that will help you help us follow the code easier.

I can't really tell, but "however I still can't get the sentinel to work properly but everything else seems to be working properly" kinda sounds like you might be asking for help. If so, be specific and explain.

Ok here is a little better format. Yes I would like some help on trying to help me figure out why when I type Q to exit the program per SENTINEL that it just prints Q and keeps repeating in the loop. I have tried SENTINEL[2]="Q";, SENTINEL = "Q"; and SENTINEL 'Q'; declaring SENTINEL as an array allows the code to compile, the other two declarations yield errors. For SENTINEL = "Q"; it yields cannot convert cannot convert from 'const char [2]' to 'const char' and for SENTINEL = 'Q'; it yields error C2446: '!=' : no conversion from 'int' to 'char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast, char [11]' differs in levels of indirection from 'int'. Any help in determining what I have wrong I guess it is with my declaration and comparison of my input to SENTINEL is greatly appreciated.

const int MAX = 11;
char arrayInput[MAX];
void cStringToUpper (char[]); 
const char SENTINEL[2] = "Q";
char arrayOutput[MAX];

int main()
{
		
	do
	{
		cout << "\n" << "Get Input: ";
		cin >> arrayInput;

	{
		cStringToUpper(arrayInput); //Call Convert Funtion
		cout << arrayInput;			//Print Converted Char Array
	}	
	}while (arrayInput != SENTINEL);
	
	cin.ignore(2);
	return 0;
}



void cStringToUpper (char arrayInput[])  
{
	int size=0;
	
	while (arrayInput[size] != '\0') //while the array does not equal the  null												term, carry out the convert function
		{
		if (arrayInput[size] >= 97 && arrayInput[size] <= 122) //Determine if input																	letters are lower case
			arrayInput[size] = (arrayInput[size])-32; //Capitalize Lower Case Letters
		
		else
	
			arrayInput[size] = arrayInput[size]; //Print all other letters as they													were entered
			size++; 
		}
}

Use strcmp in the <cstring> header. You can't compare two C-strings using ==.

if((strcmp(string1,string2) == 0)
   cout<<"Strings are the same"<<endl;

Easier would be to use cin.get to read in a character and compare it to 'Q'

Excellent :) I was not able to use cin.get() to function properly so i left it at cin>>.....;

However moving my while statement to the stop and stating while (strcmp(arrayInput,SENTINEL) != 0)
{........}
Worked perfectly. Thanks for the help everyone.

Ok here is a little better format.

Much better. You still need more consistency though. Indent the same way throughout the program and watch for really long lines:

const int MAX = 11;
char arrayInput[MAX];
void cStringToUpper (char[]); 
const char SENTINEL[2] = "Q";
char arrayOutput[MAX];

int main()
{
        
    do
    {
        cout << "\n" << "Get Input: ";
        cin >> arrayInput;
        {
            cStringToUpper(arrayInput);             // Call Convert Funtion
            cout << arrayInput;                     // Print Converted Char Array
        }    
    } while (arrayInput != SENTINEL);
    
    cin.ignore(2);
    return 0;
}



void cStringToUpper (char arrayInput[])  
{
    int size=0;
    
    while (arrayInput[size] != '\0')        // while the array does not equal the  null
    {                                       //    term, carry out the convert function
        if (arrayInput[size] >= 97 && arrayInput[size] <= 122)  // Determine if input
        {                                                       //    letters are lower case
            arrayInput[size] = (arrayInput[size])-32;           // Capitalize Lower Case Letters
        }
        else
        {
            arrayInput[size] = arrayInput[size];                // Print all other letters
            size++;                                             //    as they were entered
        }
}
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.