Very simple function I can't execute - c++

I am a student and I'm trying to write a function that returns "true" if I enter a vowel, and "false" if I enter a consonant.
This is what I wrote:
#include <iostream>
using namespace std;
int main()
{
bool isVowel(char x);
}
bool isVowel(char x)
{
cout << "Enter a letter" << endl;
cin >> x;
if (x == 'a', 'e', 'i', 'o', 'u')
return true;
else
return false;
}
I compiled with g++ isvowel.cpp -o isvowel, and apparently there were no errors, but then I tried to execute using ./isvowel and nothin happened.
What's wrong?

Your if equality comparison does not work. As Sam stated in the comments, you need to exlicitely compare x with each value.
bool isVowel(char x)
{
// The if is not really needed here since you can
// just return the result of the comparison
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') {
return true;
}
else {
return false;
}
}
To call this method:
std::cout << "Input character" << std::endl;
char x;
std::cin >> x;
bool vowel = isVowel(x);
std::cout << "Is vowel: " << vowel << std::endl;

The comma doesn't work that way you think (it's been explained above), but the reason that nothing happens is because you don't really call the isVowel function. The code
bool isVowel(char x);
defines a new function that hides the one you meant to call. To make this work, you need to call the function correctly, i.e.
isVowel('x'); // or, better, isVowel(), as I'll explain below
and move the isVowel function definition above main, because otherwise it wouldn't be visible inside main. The third thing is, the "char x" parameter you pass to the function doesn't really do anything, since you overwrite it with cin >> x; You probably want to use a method that takes no parameters. And finally, if you want to see the result, you need to print it to the screen. So, to put this together, your code should look like:
#include <iostream>
using namespace std;
bool isVowel()
{
cout << "Enter a letter" << endl;
char x;
cin >> x;
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
return true;
else
return false;
}
int main()
{
bool b = isVowel();
cout << b << endl;
}
or, if you prefer,
#include <iostream>
using namespace std;
bool isVowel(char x)
{
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
return true;
else
return false;
}
int main()
{
cout << "Enter a letter" << endl;
char x;
cin >> x;
bool b = isVowel(x);
cout << b << endl;
}

Related

executing ls command and printing output it

so i have made this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
while(true)
{
string x;
string y;
string an1[3] = {"quit","exit","leave"};
double ver = 0.1;
cout << "basic0.1% ";
cin >> x;
if (x == "upgrade")
{
system("yay -Syyu");
}
else if (x == "install")
{
cout << "What do you want to install?" << "\n";
cin >> y;
system(("sudo yay -S " + y).c_str());
}
else if (x == "version")
{
cout << "version: " << ver << "\n";
}
else if (x == "clear")
{
system("clear");
}
else if(x == "quit" || "leave" || "exit")
{
break;
}
else if(x == "ls")
{
}
}
return 0;
}
and in the else if(x == "ls"){} i wanted to show the output of the command,i tried putting system("ls");
but it just exits the program.
btw this should be a little terminal emulator for linux but i am not sure yet,i am just having some fun.
system("ls"). works fine. Your problem is this:
else if(x == "quit" || "leave" || "exit")
{
break;
}
Let's say you type ls. Then x contains the string "ls". x == "quit" is false because you didn't type quit. "leave" is true because it's a non-null pointer. "exit" is true because it's a non-null pointer. false || true || true is true, so the program quits.

Having trouble with a 'while' loop in C++

I started building a very simple version of a calculator in C++. The idea is to perform basic operations with only two numbers and then loop back so the user can make a new calculation.
The program looks like this:
#include<iostream>
#include<string>
#include"mathOperations.h"
using namespace std;
int main()
{
int x, y;
string operation;
string repeat = "y";
while (repeat == "y" or "Y")
{
cout << "Welcome! This is a raw version of a calculator - only use two numbers." << endl;
cin >> x >> operation >> y;
if (operation == "+")
{
cout << "Result: " << add(x, y) << endl;
}
else if (operation == "-")
{
cout << "Result: " << subtract(x, y) << endl;
}
else if (operation == "*")
{
cout << "Result: " << multiply(x, y) << endl;
}
else if (operation == "/")
{
cout << "Result: " << divide(x, y) << endl;
}
else
{
cout << "This is not a valid sign. Please choose another one!" << endl;
}
cout << "Wanna go again? Type 'y' or 'n'." << endl;
cin >> repeat;
if (repeat == "n" or "N")
{
cout << "Alright, have a nice day!" << endl;
break;
}
}
}
int add(int x, int y)
{
return x + y;
}
int subtract(int x, int y)
{
return x - y;
}
int multiply(int x, int y)
{
return x * y;
}
int divide(int x, int y)
{
return x / y;
}
NOTE: There is a 'mathOperations.h' file in which I have made forward declarations of all functions used.
The problem is that whenever I type in 'y' to make it loop, it simply outputs the following 'if' statement and breaks out of the loop and the program finishes. I couldn't quite figure out why this is happening, since the 'if' statement is only supposed to run if I type in 'n'.
repeat == "n" or "N"
evaluates to
(repeat == "n") || "N"
see the C++ operator precedence.
The first repeat == "n" evaluates to true or false depending on your input, but the second clause of the OR, i.e. "N", always evaluates to true because it is a string literal that decays to a non-zero const char* pointer, and in C or C++ everything non-zero is implicitly converted to true. So your OR clause is always true, which implies that the if block will always be executed.
As mentioned in the comments, you need to do
if(repeat == "n" || repeat == "N") {...}
Similarly with the first while condition.
Nice code! I try using "||" in place of your "or" in your if statements. Might want to refresh your knowledge with C++ short-circuiting of booleans.

Writing a Taste test program for C++ not incrementing

I am writing a taste test program for a class and it will run fine up until my if statements. However, once I get into my if statements it isn't incrementing i, so it never leaves the while loop.
#include <iostream>
using namespace std;
void main()
{
int i = 0;
int q = 0;
int p = 0;
int c = 0;
char preference;
int x = 0;
cout << "How many taste tests would you like to do?" << endl;
cin >> x;
while (i<x)
{
cout << "Do you prefer Coke, Pepsi, or are they the same? Use c for coke, p for pepsi, and q for the same\n";
cin >> preference;
if (preference == 'q' || preference == 'Q')
{
q = q + 1;
i++;
}
if (preference == 'p' || preference == 'P')
{
p = p + 1;
i++;
}
if (preference == 'c' || preference == 'C')
{
c = c + 1;
i++;
}
}
if (p>q)
{
cout << "Pepsi wins" << endl;
if (c>p)
cout << "Coke wins" << endl;
if (c == p)
cout << "Tie" << endl;
}
}
I think that you are trying to check if the a determined char was clicked by cin >> preference but you are test int vars, so if i press 'q'
//if(preference==q || preference==Q) // 'q'==0 || 'p'==0
if(preference=='q' || preference=='Q') // 'q'=='q' || 'q'=='Q'
so add ''
I hope this helps
Change the following lines in the code for the expected behavior.
//if(preference==q || preference==Q)
if(preference=='q' || preference=='Q'
//if(preference==p || preference==P)
if(preference=='p' || preference=='P')
//if(preference==c || preference==C)
if(preference=='c' || preference=='C')

What is wrong with getchar (as program is exiting without a check at do while loop)?

In this in the main function the usage of getchar in do while loop is creating problem (as per what i m figuring out) and using getch resolves it..plz help why so..
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
const int size = 10;
int main()
{
int stack[size];
int top = -1;
char ch;
char chh;
do {
cout << "what you want to do? 1:Push,2:Pop,3:Display \n";
cin >> ch;
if (ch == '1')
{
int a;
cout << "enter element to be entered";
a = getchar();
int r = push(stack, &top, a);
if (r == -1) cout << "array already full \n";
}
if (ch == '2')
{
pop(stack, &top);
}
if (ch == '3')
{
display(stack, &top);
}
cout << "enter y to continue";
chh = getchar();
} while (chh == 'y');
return 0;
}
Some else clauses will help. Put all those if's into one big if/else if/else loop:
if (ch == '1') { ... }
else if (ch == '2') { ... }
else if (ch == '3') { ... }
else { /*print out the bad char*/ }
You're likely getting a character that you're not expecting, like a carriage return.
Also, why are you mixing cin and getc?

C++ Boolean function returning 56

I have a function to return a bool:
bool restart()
{
std::string answer;
bool answered = false;
while(answered == false)
{
cout << endl << endl << "Do you want to play again? y/n : ";
cin >> answer;
if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
{return true;}
if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
{return false;}
}
}
When I call it using:
cout << restart();
I get the output:
Do you want to play again? y/n : y
56
Can anyone see how to fix this strange problem?
Thanks in advance.
My WIP code as it is now:
#include <iostream>
#include <cstdlib>
using namespace std;
void drawScreen(int grid[3][3]); //
int movef(int grid[3][3], bool playersMove);
int updateGrid(int grid[3][3], int move, bool playersMove);
bool hasWon(int grid[3][3]);
bool swapMover(bool playersMove); //
bool restart();
void endGame();
int main()
{
int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
bool appRunning = true, gameRunning = true, playersMove = true;
int move;
// tests //
std::cout << restart();
// tests //
//while(appRunning == true)
//{
// while(gameRunning == true)
// {
// drawScreen(grid);
// move = movef(grid, playersMove);
// grid[3][3] = updateGrid(grid, move, playersMove);
// drawScreen(grid);
// gameRunning = hasWon(grid);
// playersMove = swapMover(playersMove);
// }
// appRunning = restart();
//}
//endGame();
}
void drawScreen(int grid[3][3])
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(grid[i][j] == 10){cout << "X ";if(j == 2){cout << endl << endl;}}
if(grid[i][j] == 11){cout << "O ";if(j == 2){cout << endl << endl;}}
if(grid[i][j] != 10 && grid[i][j] != 11){cout << grid[i][j] << " ";
if(j == 2){cout << endl << endl;}}
}
}
}
int movef(int grid[3][3], bool playersMove)
{
return 0;
}
int updateGrid(int grid[3][3], int move, bool playersMove)
{
return 0;
}
bool hasWon(int grid[3][3])
{
return false;
}
bool swapMover(bool playersMove)
{
if(playersMove == true){return false;}
if(playersMove == false){return true;}
}
bool restart()
{
std::string answer;
bool answered = false;
while(answered == false)
{
cout << endl << endl << "Do you want to play again? y/n : ";
cin >> answer;
if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
{return true;}
if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
{return false;}
}
}
void endGame()
{
}
[This is somewhat of a repost of my comment since OP said it solved his problem]
You have no return value defined outside of the while loop. If somehow you get outside of it, you do not return a value (though I have no idea what behavior is expected or even if any behavior is expected in this case)
To make my answer a bit more thorough, I have found this:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
OK, I'm making a guess. Your original code has the line
grid[3][3] = updateGrid(grid, move, playersMove);
And your grid definition is
int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
This means that your are writing out of the array bounds. This is undefined behavior.
Please correct this and check if your program works as expected.
I used the mingw32 compiler, when using g++ 4.8 it works fine. It seems to be a compiler error. (thanks to grizzly)