I have written a short program that takes a user input and then checks a string for a match to the users input but I need to add another function that checks make sure that the user input is in the string and if its not to return an error.
Here is my code for reference:
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
int main()
{
char letter; //Variable holding user entered letter
cout << "Please enter letter in the aplhabet:" << endl;
cin >> letter;
cout << "The Position of " << letter << " in the string is: " << ALPHABET.find(letter) << endl;
return 0;
}
I think I ought to add an if/else statement that first checks to see if the input is correct and if it is output the position in the string and if not return and error.
If you wanted to be fancy, you could write your own function. However, string::find() is ok. All you need to check is whether or not the returned index is valid or not.
// Example program
#include <iostream>
#include <string>
using namespace std;
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
int main()
{
char letter; //Variable holding user entered letter
string::size_type index; //Index where char is found in string
cout << "Please enter letter in the aplhabet:" << endl;
cin >> letter;
index = ALPHABET.find(letter);
if (index == string::npos)
cout << "Error, letter not found" << endl;
else
cout << "The Position of " << letter << " in the string is: " << index << endl;
return 0;
}
An if/else statement sounds good. And if that doesn't work, there are other multiple ways that it could.
Related
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;
}
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 am working on an assignment for my C++ class. The following code is given. The directions explain to enter a six character string and observe the results. When I do this, the second user prompt is passed over and the program ends. I am pretty certain the reason for this is that the first cin.getline() is leaving the extra character(s) in the input stream which is messing up the second cin.getline() occurrence. I am to use cin.get, a loop, or both to prevent the extra string characters from interfering with the second cin.getline() function.
Any tips?
#include <iostream>
using namespace std;
int main()
{
char buffer[6];
cout << "Enter five character string: ";
cin.getline(buffer, 6);
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
cout << "Enter another five character string: ";
cin.getline(buffer, 6);
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
return 0;
}
You are right. The newline character stays in the input buffer after the first input.
After the first read try to insert:
cin.ignore(); // to ignore the newline character
or better still:
//discards all input in the standard input stream up to and including the first newline.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
You will have to #include <limits> header for this.
EDIT:
Although using std::string would be much better, following modified code works:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
char buffer[6];
cout << "Enter five character string: ";
for (int i = 0; i < 5; i++)
cin.get(buffer[i]);
buffer[5] = '\0';
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
cout << "Enter another five character string: ";
for (int i = 0; i < 5; i++)
cin.get(buffer[i]);
buffer[5] = '\0';
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
return 0;
}
So, I'm just starting out this C++ course and we are doing strings now. For this assignment, what my professor wants me to do is to find a string within a string and to print it out and at a position. This is my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Please enter a phrase: " << endl;
string phrase;
getline(cin, phrase);
cout << "Please enter a possible substring of the phrase: " << endl;
string phrase_2;
getline(cin, phrase_2);
string pos = phrase.substr(phrase_2);
cout << phrase_2 << "was found at position " << pos << endl;
return 0;
}
I have tried multiple hours trying to get the code to print out the position. This might be totally wrong and I apologize for that, but if you could help me out, I would appreciate it.
You need to use std::string::find to get the position of a sub string within a string:
Using your code as an example:
int main ()
{
cout << "Please enter a phrase: \n";
string phrase;
getline(cin, phrase);
cout << "Please enter a possible substring of the phrase: \n";
string phrase_2;
getline(cin, phrase_2);
std::size_t position = phrase.find(phrase_2);
if (position != std::string::npos)
std::cout << phrase_2 << " was found at position " << position << "\n";
return 0;
}
Google is your friend...
Instead of
string pos = phrase.substr(phrase_2);
You should use
size_t pos = phrase.find(phrase_2);
Consider the following code which takes an integer input and then prints the cin stream state:
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Enter a number \n";
cin>>number;
cout<<cin.rdstate()<<endl;
return 0;
}
If the number entered is "zzzz" then the rdstate returns a value of 4.
If the number entered is "10zzzz" then the rdstate returns a value of 0, number has a value of 10, and the input stream has "zzzz" in it.
My question is:
1. Why isn't a input of "10zzzz" treated as an invalid input (atleast one of the failure bits should have been set.)
2. What is an elegant solution to detect and handle this situation.
Thanks!!!
First of all I would like to ask what you are trying to do with:
cout<<cin.rdstate()<<endl;
Read this page for the proper use of rdstate()
http://www.cplusplus.com/reference/iostream/ios/rdstate/
second:
to check wetether the input is either stringtype or integer type you might want to add something extra wich will convert the input string to integer data and will respond with an error message when feeded an invalid input.
therefor this will help you out:
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;
}