Do if statements work this way? This is a "guess the number" game. The 1st if says to go higher/lower, the 2nd if says if you're within a 50, 100 or 100+ range.
Both are supposed to work simultaneously, but I get an error.
Line 37 unexpected primary expression before '| |' token, Line 38
expected ';' before 'cout'
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <cstdio>
using namespace std;
int main()
{
int x;
cout << "Please enter a number\n";
srand(time(0));
int y = rand();
while (x != y)
{
cin >> x;
{
if (!(cin.good())) //1st if
{
cout << "No letters noob" << endl;
cin.clear();
cin.sync();
}
else if (x < y)
cout << "Go higher" << endl;
else if (x > y)
cout << "Go lower" << endl;
else
cout << "You win!!" << endl;
}
{
if (y - x - 50 <= 0) || (x - y - 50 <= 0) //2nd if
cout << "within 50 range" << endl;
else if (y - x - 100 <= 0) || (x - y - 100 <= 0)
cout << "within 100 range" << endl;
else
cout << "100+ value away" << endl;
}
}
cin.get();
getchar();
return 0;
}
You are missing parentheses.
For example, this line:
if (y - x - 50 <= 0) || (x - y - 50 <= 0)
Should read:
if ((y - x - 50 <= 0) || (x - y - 50 <= 0))
Because the entire if condition must be wrapped in parentheses.
Looks like you may have some other issues there as well.
In addition to the correct answer by #jonathan-wood, the following may express your intent more clearly:
#include <cstdlib>
...
const int off_by = abs(x - y);
if (off_by <= 50) {
...
} else if (off_by <= 100) {
...
}
FYI: if you think it would improve your code's readability, you can also use "or" and "and" instead of "||" and "&&". So, the following is legal:
if ((y - x - 50 <= 0) or (x - y - 50 <= 0)) {
...
}
Related
I previously asked for help with a school assignment after getting help from several of the good people of stack overflow I've hit another snag. For some reason my program wont print an odd number of * to the console so I get a miss shaped diamond. Would anyone have any ideas on how I can fix that issue?
Here is my code:
using namespace std;
int x;
int y;
int rowNum;
int main (int argc, char *argv[]){
cout << " Enter the Numbers of rows you would like between 3 and 15: " << endl;
cin >> rowNum;
if((3 <= rowNum)&&( rowNum<= 15))
{
for (x = 1; x <= rowNum; x++) //outer loop
{
for (y = 1; y <= x; y++) {
if (y <= (rowNum - x))
cout << " ";
else
cout << "*";
}
cout << endl;
}
for (x = rowNum; x >= 1; x--) //outer loop
{
for (y = 1; y <= x; y++)
{
if (y <= (rowNum - x))
cout << " ";
else
cout << "*";
}
cout << endl;
}
}
else{
cout << "invalid number try again";
}
return 0;
}
Trying to make a calculator that calculates values in an array based on input from user. But the first value in the array is always 0 when I leave 'p undefined or p = 1 will have give me the same problem. It should be whatever the user enters for the first value and so on.
#include <iostream>
using namespace std;
int main() {
double x;
int p = 1, y = 0;
double sum = 1;
int many[p];
char op;
cout << "How many numbers are you working with today?" << endl;
cin >> x;
do
{
if (y > x)
break;
cout << "Enter number " << y + 1 << ":" << endl;
cin >> many[p];
cout << "What would you like the numbers to do: (+ - / *)" << endl;
cin >> op;
if (op == '+')
{
sum+=many[p];
cout << sum <<endl;
}
else if (op == '-')
{
sum-=many[p];
cout << sum <<endl;
}
else if (op == '*')
{
sum*=many[p];
cout << sum <<endl;
}
else if (op == '/')
{
sum/=many[p];
cout << sum <<endl;
}
else {cout << "ERROR: Enter correct value." << endl;}
y++;
}
while (y < x);
}
The sum should be 3 not 4.
How many numbers are you working with today?
2
Enter number 1:
1
What would you like the numbers to do: (+ - / *)
+
Enter number 2:
2
What would you like the numbers to do: (+ - / *)
+
4
The program is invalid and has undefined behavior.
For starters variable length arrays is not a standard C+ feature
int p = 1, y = 0;
double sum = 1;
int many[p];
And in any case you defined an array with one element. So the only valid index to access elements of the array is 0.
Even in the first statement that uses the array
cin >> many[p];
it is accessed outside its bounds.
You should use the standard class template std::vector. Or as in fact you are dealing with one value then there is even no sense to use a container, Define a scalar object instead of the array.
The initial value of the sum is 1, that's why it is adding 1 more. We can't keep it 0 either, because then it will mess up the '*' and '/' cases.
I have added the initial sum value for all the cases.
Also, I would suggest you, to use switch cases instead of if, else statements.
#include <iostream>
using namespace std;
int main() {
double x;
int p = 1, y = 0;
double sum = 1;
int many[p];
char op;
cout << "How many numbers are you working with today?" << endl;
cin >> x;
do
{
if (y > x)
break;
cout << "Enter number " << y + 1 << ":" << endl;
cin >> many[p];
cout << "What would you like the numbers to do: (+ - / *)" << endl;
cin >> op;
if (op == '+')
{
if (y == 0) {
sum = 0;
}
sum+=many[p];
cout << sum <<endl;
}
else if (op == '-')
{
if (y == 0) {
sum = 0;
}
sum-=many[p];
cout << sum <<endl;
}
else if (op == '*')
{
if (y == 0) {
sum = 1;
}
sum*=many[p];
cout << sum <<endl;
}
else if (op == '/')
{
if (y == 0) {
sum = 1;
}
sum/=many[p];
cout << sum <<endl;
}
else {cout << "ERROR: Enter correct value." << endl;}
y++;
}
while (y < x);
}
There are a lot of things here that don't make sense.
You are starting with sum = 1. this is why the value is always +1
many is an array of size 1, can be changed to single int.
you are accessing many[p] which is many[1] which is out of bounds. you can only access many[0]
the rest I leave it to you to find,
So basically I am trying to get it to stop repeating. If I enter numbers correctly it works fine. If I enter negative numbers which are not allowed and needs a try-catch exception it keeps repeating and won't stop asking for numbers.
All I have is this source file for the code and I am trying to make a function for main.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void gcd(int x, int y);
int main()
{
int x;
int y;
cout << "Please enter two integer values" << endl;
cin >> x;
cin >> y;
gcd(x, y);
return 0;
}
void gcd(int x, int y)
{
int gcd;
int s = 0;
while (s == 0)
{
try
{
if (x < 0 || y < 0)
throw 1;
else
{
s == 1;
break;
}
}
catch (int x)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
cin >> x >> y;
continue;
}
}
for (int i = 1; i <= x && i <= y; i++)
{
if (x % i == 0 && y % i == 0)
gcd = i;
}
cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd << endl;
}
If you don't want your function gcd() to be called with negative values, throw a std::invalid_argument exception. It is not the business of gcd() to request user input. Validate the input in main() before you call gcd().
#include <limits>
#include <stdexcept>
#include <iostream>
int gcd(int, int);
int main()
{
int x, y;
while (std::cout << "Please enter two positive integers: ",
!(std::cin >> x >> y) || x < 0 || y < 0)
{
std::cerr << "Input error :(\n\n";
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
std::cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd(x, y) << "\n\n";
}
int gcd(int x, int y)
{
if (x < 0 || y < 0)
throw std::invalid_argument("No negative arguments to gcd(), please :(");
return y == 0 ? x : gcd(y, x % y);
}
You can (and perhaps should) remove the logic from gcd function and instead place it where you get your input from user, that is, in main. Also, state the requirements up front. For example:
int main()
{
int x;
int y;
cout << "Please enter two positive integer values" << endl;
cin >> x;
cin >> y;
if (x < 0 || y < 0)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
return 0;
}
gcd(x, y);
return 0;
}
Now, you can place assertions in gcd to enforce no negative values get in:
void gcd(int x, int y)
{
assert(x >= 0);
assert(y >= 0);
// ...
}
#include "stdafx.h"
#include "math.h"
#include <string>
#include <iostream>
using namespace std;
int main ()
{
int x;
cout << "Enter a number." << endl;
cin >> x;
int y = 1;
int i = 0;
while (i == 0 && y < sqrtf(x))
{
if (fmodf(x,y) == 0)
{
i = 1;
}
else
{
i = 0;
}
y++;
if (i == 1)
{
cout << "Your number is prime." << endl;
}
else
{
cout << "Your number is composite." << endl;
}
}
return 0;
}
This is a code I created to test for primes. After sorting out several debugging issues I was able to run it.
It opened the command window, read 'Enter a number' and closed itself the second I entered a number.
Help?
You have to:
close the while loop in the correct place
change the if (i == 1) condition (i==1 means x is divisible by some y)
start with y = 2 (every number is divisible by one)
include sqrtf(x) in the loop (y <= sqrtf(x) or 15, 25, 35... are primes).
So:
int main()
{
int x;
cout << "Enter a number." << endl;
cin >> x;
int y = 2; // <-- changed
int i = 0;
while (i == 0 && y <= sqrtf(x)) // <-- changed
{
if (fmodf(x,y) == 0)
{
i = 1;
}
else
{
i = 0;
}
y++;
} // <-- moved here
if (i == 0) // <-- changed
{
cout << "Your number is prime." << endl;
}
else
{
cout << "Your number is composite." << endl;
}
return 0;
}
works (more or less...).
Anyway:
don't use using namespace std; (Why is "using namespace std" considered bad practice?)
\n should be your default ("\n" or '\n' or std::endl to std::cout?)
y and x are integers so you can use % instead of fmodf
avoid premature pessimization: prefer preincrement, only use postincrement if you're going to use the original value
else { i = 0; } is superfluous
you can change y < sqrtf(x) with y * y <= x (and you don't need math.h anymore) or find square root of number then start the loop
Somewhat better (but far from perfect):
#include <cmath>
#include <iostream>
int main()
{
int x;
std::cout << "Enter a number.\n";
std::cin >> x;
int square_root = std::sqrt(x);
int y = 2;
int i = 0;
while (i == 0 && y <= square_root)
{
if (x % y == 0)
i = 1;
++y;
}
if (i == 0)
std::cout << "Your number is prime.\n";
else
std::cout << "Your number is composite.\n";
return 0;
}
Now:
input validation, i.e. check for bad input values (How to check if input is numeric in C++)
special cases checking (is the number one a prime number?)
a bool would express your intentions better than int i;
improve the algorithm (Determining if a number is prime, Which is the fastest algorithm to find prime numbers?, Primality tests)
The Question :
If the sequence of digits in X forms a substring of the sequence of digits in Y, it outputs "X is a substring of Y"; otherwise, if the sequence of digits in X forms a subsequence of the sequence of digits in Y, then it outputs "X is a subsequence of Y"; otherwise, it outputs "X is neither substring nor subsequence of Y".
DO NOT use arrays or strings in this program
The output should be as follows
Enter Y : 239847239
Enter X : 847
X is substring of Y
Enter Y : 239847239
Enter X : 3923
X is subsequence of Y
Enter Y : 239847239
Enter X : 489
X is neither substring nor subsequence of Y
And below is what I got so far... (haven't coded anything for subsequence as i was clueless)
I know my coding is very unefficient and is only suitble to use for the above model output. Any improvements or comments how to fix this would be greatly appreciated.
#include <cmath>
#include <iostream>
using namespace std;
int main() {
cout << "Enter Y: " ;
int Y;
cin >> Y;
cout << "Enter X: ";
int X;
cin >> X;
if (Y >= X){ // Below are all the possibilities of substrings up to 9 decimal places.
if( X == (Y % 10)) cout << "X is substring of Y";
else if (X == (Y % 100)) cout << "X is substring of Y";
else if (X == (Y % 1000)) cout << "X is substring of Y";
else if (X == (Y % 10000)) cout << "X is substring of Y";
else if (X == (Y % 100000)) cout << "X is substring of Y";
else if (X == (Y % 1000000)) cout << "X is substring of Y";
else if (X == (Y % 10000000)) cout << "X is substring of Y";
else if (X == (Y % 100000000)) cout << "X is substring of Y";
else if (X == (Y % 1000000000)) cout << "X is substring of Y";
else cout << "X is neither substring nor subsequence of Y";
}
else cout << "neither subsequence nor subset"; // prints out when Y is less than X.
return 0;
}
This what i got by far:
Substring
bool substring(long x, long y){
double auxX = x;
int power = 0;
while (auxX >= 1){
auxX /= 10;
++power;
}
int mask = pow(10, power);
int reminder;
while (y > 0){
reminder = y % mask;
if (reminder == x){
return true;
}
y /= 10;
}
return false;
}
Subsequence
bool subsequence(long x, long y){
int lastX;
int lastY;
while (y > 0){
lastX = x % 10;
lastY = y % 10;
if (lastX == lastY){
x /= 10;
if (x <= 0){
return true;
}
}
y /= 10;
}
return false;
}
Main
int main()
{
long x = 21;
long y = 469721481;
if (substring(x, y)){
cout << x << " is substring of " << y << std::endl;
}
else if (subsequence(x, y)){
cout << x << " is subsequence of " << y << std::endl;
}
else{
cout << x << " is neither substring nor subsquence of " << y << std::endl;
}
return 0;
}
I've tested some cases and it seems to work fine, but you should test with edge cases (multiple repetitions of the same pattern, one digit numbers, zeros, etc) and retouch this snippet