I am working on a small program that takes uppercase letters and converts them to lowercase. I have this accomplished, but when outputting the result it shows before my text when it should be after. How should I go about fixing this?
Thank you.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string users_word;
cout << "Please enter a word: ";
getline(cin, users_word);
cout << "You entered the word: " << users_word << endl;
char i = 0;
char c = 0;
while (users_word[i])
{
c = users_word[i];
putchar(tolower(c));
i ++;
}
cout << "Your word in lowercase is: " << c << endl;
}
The output is:
Please enter a word: Hello
You entered the word: Hello
helloYour word in lowercase is:
I am trying to figure out how to get "hello" afterwards.
By calling putchar() directly, you are bypassing any buffering that std::cout does internally. You are outputting characters to the terminal before the cout buffer is flushed to the terminal.
Also, even if you were using std::cout instead of putchar(), you are still outputting the lowercase characters before you output "Your word in lowercase is: ". You need to output that message before entering your loop, not outputting it after the loop.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string users_word;
cout << "Please enter a word: ";
getline(cin, users_word);
cout << "You entered the word: " << users_word << endl;
cout << "Your word in lowercase is: ";
for (string::size_type i = 0; i < users_word.size(); ++i)
{
char c = users_word[i];
//putchar(tolower(c));
cout.put(tolower(c));
// or: cout << (char) tolower(c);
}
cout << endl;
}
Actually the while loop implicitly redirects the lower chars to std::out already.
Thus,
cout << "Your word in lowercase is: " << c << endl;
will appear your lower text in front of Your word in lowercase is:. Just move the cout << "Your word in lowercase is: "; before while statement.
#include <iostream>
#include <string>
using namespace std;
int main()
{
...
cout << "Your word in lowercase is: ";
while (users_word[i])
{
c = users_word[i];
putchar(tolower(c));
i++;
}
cout << endl;
}
Related
I am having trouble determining if an input is a letter or a number.
If I enter anything it always says that it is not a number, what am I doing wrong.
Here is my code:
#include <iostream>
#include <unistd.h>
#include <ctype.h>
using namespace std;
int main()
{
int input = 0;
cout << "Enter a number \n";
cout << "input: ";
cin >> input;
if (isdigit(input)) {
cout << "Your number is: " << input;
}
else {
cout << "This is not a number \n";
}
//wait for ten seconds
usleep(10000000);
}
Since isdigit() expects an ASCII value as its argument, it will return true only if you type in a number between 48 (aka the ASCII code for "0") and 57 (aka the ASCII code for "9"), which isn't what you want.
In order to get the behavior you want, you'll need to read the user's input into a string, and then analyze the contents of the string to see if they reasonably represent an integer or not. Something like this:
#include <iostream>
#include <string>
#include <unistd.h>
#include <ctype.h>
using namespace std;
int main()
{
string inputStr;
cout << "Enter a number \n";
cout << "input: ";
cin >> inputStr;
// Assume a string counts as representing an integer
// if the first character of the string is an ASCII digit.
// (You might also want to accept strings where the
// first character is a + or - symbol and is immediately
// followed by an ASCII digit, but for simplicity I'm
// omitting that logic here)
if ((inputStr.length()>0)&&((isdigit(inputStr[0])))) {
int number = stoi(inputStr);
cout << "Your number is: " << number << endl;
}
else {
cout << "[" << inputStr << "] is not a number" << endl;
}
//wait for ten seconds
usleep(10000000);
}
Agree on Jeremy, I would like to add https://stackoverflow.com/a/5655685/7637661 for reference.
TLDR
#include <iostream>
using namespace std;
int main() {
int input = 0;
cout << "Enter a number \n";
cout << "input: ";
cin >> input;
if(!cin) // or if(cin.fail())
{
// user didn't input a number
cin.clear(); // reset failbit
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //skip bad input
// next, request user reinput
cout << "This is not a number " << endl;
}
cout << "Your number is: " << input;
}
I am new here and new to c++ as well.
I just started my first year at school and I have been given an assignment in which one of the questions is to convert an octal number to a decimal number using Char only.
The task is to create a program that receives chars from the user and where the length of the number is not known in advance. The user should press '\t' in order to start calculating to a decimal number.
I don't really understand how it works.Because if I code a simple algorithm such as:
char ch;
cin<<ch;
cout>>ch>>endl;
and I give it 67, it will print 6 only. That means that it reads every char separately, doesn't it?
Could someone please help me understand it by showing me the algorithm for this problem or explaining to me how char works?
Thanks a lot
Coral
You will get enough info on how to read from stdin char by char.
please go through this link.
http://www.cplusplus.com/forum/articles/6046/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string input = "";
// How to get a string/sentence with spaces
cout << "Please enter a valid sentence (with spaces):\n>";
getline(cin, input);
cout << "You entered: " << input << endl << endl;
// How to get a number.
int myNumber = 0;
while (true) {
cout << "Please enter a valid number: ";
getline(cin, input);
// This code converts from string to number safely.
stringstream myStream(input);
if (myStream >> myNumber)
break;
cout << "Invalid number, please try again" << endl;
}
cout << "You entered: " << myNumber << endl << endl;
// How to get a single char.
char myChar = {0};
while (true) {
cout << "Please enter 1 char: ";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
break;
}
cout << "Invalid character, please try again" << endl;
}
cout << "You entered: " << myChar << endl << endl;
cout << "All done. And without using the >> operator" << endl;
return 0;
}
I have one string for example, " Today is Monday " if the user types "o", the program must output "Today and Monday" because they contain "o".
My code is this but its not working its only search substring.
#include <iostream>
#include <string.h>
using namespace std;
#define size 100
int main()
{
char str[size];
char searching_string[size];
cout << " Enter String : ";
cin.getline(str,100);
cout << " String : " << str;
cout << " Enter Search String : ";
cin.getline(searching_string,size);
cout << endl << endl;
cout << strstr(str,searching_string);
cin.get();
cin.get();
return 0;
}
Besides of the Quality of this Question.
I think strchr() is what you are looking for.
Link
Hang on.
I am trying to read in a sentence to unscramble, however something is going wrong. When I enter no character it'll print out "The sentence is" and "Decoded sentence is", but when I enter one or more characters it'll just sit there and do nothing. I don't think it could be an error with MySentence class because it does not even print "The sentence is".
#include <iostream>
#include <stdio.h>
#include "MySentence.h"
#include "Corpus.h"
#include <string>
using namespace std;
int main() {
Corpus corp;
std::cout << "The proportions are: ";
for(int i = 0; i<26; i++) {
cout << corp.proportion(i+97) <<", ";
}
cout << endl;
cout << "Enter sentence terminated by <ENTER> ";
string s= "";
getline(cin, s);
cout << "The sentence is " << s;
MySentence sent(s);
sent.decode(corp);
cout << endl << "Deoded sentence is: " << sent.sentence;
return 0;
}
As suggested by n.m. try to add an endl at the end of the line
cout << "The sentence is " << s << endl;
since it is possible that the buffer is not being flushed and the problem is in the class MySentence.
An interesting post that might help would be
Buffer flushing: "\n" vs. std::endl
#include <cctype> // Character testing and conversion
using std::cin;
using std::cout;
using std::endl;
int main() {
char letter = 0; // Store input in here
cout << endl
<< "Enter a letter: "; // Prompt for the input
cin >> letter; // then read a character
if(std::isupper(letter)) { // Test for uppercase letter
cout << "You entered a capital letter."
<< endl;
cout << "Converting to lowercase we get "
<< static_cast<char>(std::tolower(letter)) << endl;
return 0;
}
if(std::islower(letter)) { // Test for lowercase letter
cout << "You entered a small letter."
<< endl;
cout << "Converting to uppercase we get "
<< static_cast<char>(std::toupper(letter)) << endl;
return 0;
}
cout << "You did not enter a letter." << endl;
return 0;
}
Here in this example, what is the difference between using 'std::' if(std::isupper(letter)) { and not using 'std::' if(isupper(letter)) {?
I tried both and they return the same result so I'm not sure what would be the benefit of using 'std::'
Posting namezero user comment:
without the std::, you'll call a function named isupper() from the current scope, and if there isn't any, from the global namespace (::isupper()). Writing std::isupper() refers to a function names isupper() in namespace std