#include <iostream>
using namespace std;
int main ()
{
char letter;
while (cin>>letter)
{
switch (letter)
{
case 'a':
cout<<"ant"<<endl;
break;
default :
cout <<"enter only lower cases letters "<<endl;
}
}
return 0;
}
Is there any feature of c++ that ignores the characters next to first character? Because i.e., if I enter aaa it displays ant ant ant, so I want to get rid of this part. I hope you get my question.
Read a string and then switch on the first character. Like this.
int main () {
string word;
while (cin >> word) {
switch (word[0]) {
case 'a':
cout << "ant" << endl;
break;
default:
cout << "enter only lower cases letters " << endl;
break;
}
}
return 0;
}
You can treat the user input as a std::string and then just look at the first character from it for your switch statement. This will ignore anything the user inputs after the first character. I can't imagine the use case for this, but I believe this is what you're asking for.
#include <cstdlib>
#include <iostream>
int main( int argc, char *argv[] )
{
std::string word;
while (std::cin >> word)
{
char letter = word[0];
switch (letter)
{
case 'a':
std::cout << "ant" << std::endl;
break;
default:
std::cout << "please enter only lower case letters" << std::endl;
break;
}
}
return EXIT_SUCCESS;
}
Read chars repeatedly and keep track of what's been added
#include <set>
int main () {
char letter;
std::set<char> used;
while (cin >> letter) {
if (!used.insert(letter)[1]) // note returns a pair; 2nd item ([1]) is true if it didn't exist before
continue;
switch (letter) {
case 'a':
cout << "ant" << endl;
break;
default:
cout << "enter only lower cases letters " << endl;
break;
}
}
return 0;
}
Only first char will be saved
int main()
{
char c = 0;
c = getchar();
putchar(c);
return 0;
}
Related
I have just started learning c++.
I wrote this program for finding how many vowels in a sentence.
But when i run the program it asking for input before the cout and cout is working after gets()
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
char str[50];
int vow;
cout<<"Type a sentence";
gets(str);
for(int i=0; str[i]!='\0';i++){
switch(str[i]){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vow++;
}
}
cout<<"There are "<<vow<<" vowels in the string "<<str;
return 0;
}
output
hello world
Type a sentenceThere are 3 vowels in the string hello world
The problem is that std::cout is buffered, and you are not flushing the buffer to the console before calling gets(), which has no concept of std::cout or its buffer. So, you will have to flush the buffer manually, either by writing a '\n' character to the output, eg:
cout << "Type a sentence\n";
gets(str);
Or, using the std::endl or std::flush manipulator:
cout << "Type a sentence" << endl; // writes '\n' and then flushes
gets(str);
cout << "Type a sentence" << flush; // just flushes without writing '\n' first
gets(str);
Or, calling the std::cout.flush() method directly, eg:
cout << "Type a sentence";
cout.flush();
gets(str);
But, you really should not be mixing C-style I/O and C++-stye I/O. std::cin and std::cout are tie()'d together by default, so if you had used std::cin instead of gets() to read the input, then std::cin would have first flushed std::cout's buffer automatically.
Try this instead:
#include <iostream>
using namespace std;
int main() {
char str[50] = {};
int vow = 0;
cout << "Type a sentence: ";
cin.getline(str, 50);
for(int i = 0; str[i] != '\0'; ++i){
switch (str[i]){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vow++;
}
}
cout << "There are " << vow << " vowels in the string " << str;
return 0;
}
Or, using std::string instead of char[]:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int vow = 0;
cout << "Type a sentence: ";
getline(cin, str);
for(size_t i = 0; i < str.size(); ++i){
switch (str[i]){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vow++;
}
}
cout << "There are " << vow << " vowels in the string " << str;
return 0;
}
I know people will immediately look at the title and just say: "Use String" or "Char can only be length of 1" but there are problems with those.
For the first, due to later on in the code using a switch statement my variable must stay as a char so that it does not cause any errors, and for the second answer during testing I found out that even if I entered a multi-length input it would just run each character into the switch statement separately which I do not want to happen. Any help is welcomed, oh and here's the code:
char input;
do {
cout << "Please enter a number from 1 to 4.";
cin >> input;
if (sizeof(input)!=1) {
cout << "Please just enter a number";
}
else {
switch (input) {
case '1': {
cout << "One";
break;
}
case '2': {
cout << "Two";
break;
}
case '3': {
cout << "Three";
break;
}
case '4': {
cout << "Four";
break;
}
default:
cout << "Enter only a number from 1-4!";
}
}
} while ((input) != '4');
Note that I have at least attempted to use strlen and the size functions but to no avail.
Well, use string, because char can only be length of 1.
std::string input;
std::cin >> input;
if (input.size() != 1) {
std::cout << "Wrong input";
} else {
switch (input.front()) {
case '1':
// ...
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My program keeps crashing every time I enter an input even though I use a system pause method and displays the same number of vowels and consonants that is incorrect. What's going on?
int main() {
// declare vars
char ch;
int vowels = 0;
int consonants = 0;
string word = "temp";
// prompt user
cout << "Please enter a word: ";
cin >> ch;
// loop vowels and consonants
for (int i = 0; i < word.size(); i++) {
ch = toupper(word[i]);
switch (ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
break;
default:
consonants++;
}
}
// show num of vowels and consanants
cout << "Number of Vowels: " << vowels << endl;
cout << "Number of Consanants: " << consonants << endl;
// pause and exit
getchar();
getchar();
return 0;
}
Normally it's better to input a full string and then parse... but if for some reason you need to stream characters as they are entered you can use cin.get(). make sure to #include <cctype> if you want to use toupper().
char nextChar;
nextChar = cin.get();
nextChar = toupper(nextChar);
int consonants = 0;
int vowels = 0;
int words = 0;
while (nextChar != '\n')
{
switch (nextChar)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++:
break;
case ' ':
words++;
break;
default:
consonants++;
break;
}
}
cout << "There were " << consonants << " consonants and " << vowels << " vowels in " << words << " words.";
instead of ending on a newline, if you want to limit to one word simply replace (nextChar != '\n') with (nextChar != ' ').
The only change that needs to occur is the change from:
cin >> ch;
to
cin >> word;
And you code will be fine. Live example
Some chages in your code :
#include <iostream>
#include <string>
using namespace std;
int main() {
// declare vars
char ch;
int vowels = 0;
int consonants = 0;
string word = "temp";
// prompt user
cout << "Please enter a word: ";
cin >> word;
// loop vowels and consonants
for (int i = 0; i < word.size(); i++) {
ch = toupper(word[i]);
switch (ch) {
case 'A':
vowels++;
break;
case 'E':
vowels++;
break;
case 'I':
vowels++;
break;
case 'O':
vowels++;
break;
case 'U':
vowels++;
break;
default:
consonants++;
}
}
// show num of vowels and consanants
cout << "Number of Vowels: " << vowels << endl;
cout << "Number of Consanants: " << consonants << endl;
// pause and exit
getchar();
getchar();
return 0;
}
Okay, so this isn't quite an answer, but it's a bit too detailed to put into a comment...
I just ran your code and it worked fine. I added the following three libraries
#include<string>
#include<iostream>
#include<stdio.h>
And then:
using namespace std;
and then compiled with g++ filename.
Check to make sure you're libraries are up to date and that you're compiling the code correctly.
Hope this helps.
I'm having trouble converting using toupper on the first character in my string.
I used tolower(first[0]) to turn the first letter into lower case.
Why doesn't toupper(first[0]) make the first character upper case?
Also, is there a way to move the first character in a string to the last spot?
Thanks a lot in advance.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char ans;
do{
string first, last;
char first_letter, first_letter2;
cout << "This program will convert your name "
<< "into pig latin.\n";
cout << "Enter your first name: \n";
cin >> first;
cout << "Enter your last name: \n";
cin >> last;
cout << "Your full name in pig latin is ";
for(int x = 0; x < first.length(); x++){
first[x] = tolower(first[x]);
}
for(int x = 0; x < last.length(); x++){
last[x] = tolower(last[x]);
}
first_letter = first[0];
bool identify;
switch (first_letter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify = true;
break;
default:
identify = false;
}
if(identify == true){
toupper(first[0]);
cout << first << "way" << " ";
}
first_letter2 = last[0];
bool identify2;
switch (first_letter2)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify2 = true;
break;
default:
identify2 = false;
}
if(identify2 == true){
toupper(first[0]);
cout << last << "way" << endl;
}
cout << "You you like to try again? (Y/N)\n";
cin >> ans;
} while(ans == 'y' || ans == 'Y');
return 0;
}
Just a simple blunder, compare
first[x] = tolower(first[x]);
with
toupper(first[0]);
usual case of the 'can't see the obvious thing missing' syndrome... I hate those mistakes.
As for moving the first character to the end I'd usually just use substr() for a simple case:
str = str.substr(1) + str[0];
I'm trying to make my C++ program exit.
I know I can pause input with while cin >> s, but I don't know what to do to make the entire program exit.
This is my code:
int main()
{
long int l;
long int i;
char s[100000];
while (cin >> s)
{
l = strlen(s);//strlen Returns the length of the C string str.
for (i = 0; i<l; i++)
{
switch (s[i])
{
case 'W':
cout << "Q"; break;
case 'E':
cout << "W"; break;
case 'R':
cout << "E"; break;
default:
cout << ";"; break;
}
}
cout << (" ");
}
system("pause");
return 0;
}
Your program will terminate when it runs out of input.
The system("pause"); seems to imply that you're using Microsoft Windows. To signal an end-of-file condition for keyboard input on Windows, type Ctrl-Z. (For Linux and other Unix-like systems, use Ctrl-D at the beginning of a line.)
Incidentally, the program you posted is complete and will not compile. That can be corrected by adding the following lines to the top:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
When posting a question, please include the entire program.
I was able to get the program to exit by using the command exit(0); within the while loop.
Here is my finished program:
include iostream
include string
using namespace std;
int main(){
long int l;
long int i;
char s[100000];
cout << "\n Write your code in UpperCase, "
cout << "to close the program switch to LowerCase \n" << endl;
while (cin >> s)
{
l = strlen(s);//strlen Returns the length of the C string str.
for (i = 0; i<l; i++)
{
switch (s[i])
{
case 'W':
cout << "Q"; break;
case 'E':
cout << "W"; break;
case 'R':
cout << "E"; break;
default:
exit(0); break;
}
}
cout << (" ");
}
system("pause");
return 0;
}
Your loop is waiting until cin >> s returns an "end of file" (EOF). You can accomplish this on Windows by typing Ctrl+Z or on Unix-like systems such as Mac and Linux Ctrl+D.
You could also put a break character in your loop, or change the condition altogether.