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.
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.
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.