Problem with functions/loops

Hey everyone, this is my first time doing this so sorry if I mess it up somehow. I'm trying to make a program that will read a sentence and then print the sentence after making certain adjustments. It's supposed to change 2 spaces in a row to just 1 space. It should also convert all capital letters after the first letter in the sentence to lowercase. It should stop reading the sentence at the period but still print the period. So for instance the sentence "hello I am a computer Science student." should be printed as "Hello I am a computer science student." Here is my code:

#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
char i;
cout << "Please type a sentence." << endl;
cin >> i;
cin.get(i);
while (i != '.')
{
if (isupper(i))
{
cout << tolower(i) << endl;
cin.get(i);
}
else if (islower(i))
{
cout << i << endl;
cin.get(i);
}
else if (isspace(i))
{
cout << i << endl;
cin.get(i);
}
}return 0;
}


When it prints the output it won't print the first letter or the period. It also prints it all vertically on different lines instead of horizontally on the same line. I haven't added any code to capitalize the first letter or remove double spaces because I'm unsure as to how I would go about that. I'm not looking for an answer, I'm simply looking for some guidance on how to approach this.
closed account (48T7M4Gy)
Part of your problem is you are converting to lower case and returning the int value of the character and your use of endl.
if (isupper(i))
{
1
2
i = tolower(i);
			cout << i ;

cin.get(i);
}
Last edited on
Reason why its not outputting the first letter. You get the first character before your while loop like so:
1
2
cin >> i;
cin.get(i);

You never tell the program to output that first character however.

The reason why it never outputs the period is because you only tell the program to output a character if it is uppercase, lowercase, or a space. Never if it's a period.

This is why your program prints vertically:
cout << i << endl;
After every cout statement, you tell the program to endl, which means to skip to the next line, and since you call it every character, it skips a line after every character you

What I'd suggest doing to get this program to work is to obtain the entire string using either a character array/c-string or a string class. Only then would you modify the user's input.

c-string tutorial: http://www.cplusplus.com/doc/tutorial/ntcs/
C-strings reference: http://www.cplusplus.com/reference/cstring/
Last edited on
closed account (48T7M4Gy)
The reason why it never outputs the period is because you only tell the program to output a character if it is uppercase, lowercase, or a space. Never if it's a period.


Make line after while loop completed cout << '.';
Last edited on
Okay thank you all so much for the quick responses, this should help a lot for my next assignments as well.
This is my workaround for your char being represented as an ascii value (int):
1
2
3
4
5
if (isupper(i))
	{
		char ch = tolower(i);
		cout << ch;
	}

This way you don't need to store the entire input as an array and then transform it.
Last edited on
Topic archived. No new replies allowed.