Implement in C ++, using a personal library, an application that determines based on the choice made by the user if a number is positive or negative or if a number is prime or not.
This is the main code:
#include <iostream>
#include "libreria.cpp"
using namespace std;
int s;
int main()
{
int num1,cont;
cout<<"\n 1) Positive ";
cout<<"\n 2) Prime ";
cout<<"\n 3) Exit ";
cout<<"\n Choose: ";
do
{
cin>>s;
switch (s)
{
case 1:
cout<<"\nInsert the number: ";
cin>>num1;
bool sepos(int numb);
if (bool sepos(int numb)==1)
{
cout<<"\nIl numero "<<num1<<" e' positive";
}
else
{
cout<<"\nIl numero "<<num1<<" e' negative";
}
break;
case 2:
break;
}
} while (s!=3);
return 0;
}
The library is:
bool sepos(int numb)
{
if(numb>=0)
{
return true;
}
else
{
return false;
}
}
For now, I was trying to see if the number was positive or not.
But the application won't work, i got a lot of errors.
[Error] function 'bool sepos(int)' is initialized like a variable
[Error] expected primary-expression before '==' token
[Error] expected '=' before '==' token
[Warning] declaration of 'bool sepos(int)' has 'extern' and is initialized
I've noticed a couple of things you are doing wrong.
The first one is: if (bool sepos(int numb)==1) you are trying to compare a bool value ( true or false ) with the number one. Yes, C++ treats 1 and 0 like true or false but your function already returns true or false.
#include <iostream>
#include "libreria.cpp"
using namespace std;
int s;
int main()
{
int num1,cont;
cout<<"\n 1) Positive ";
cout<<"\n 2) Prime ";
cout<<"\n 3) Exit ";
cout<<"\n Choose: ";
do
{
cin>>s;
switch (s)
{
case 1:
cout<<"\nInsert the number: ";
cin>>num1;
if (sepos(int numb))
{
cout<<"\nIl numero "<<num1<<" e' positive";
}
else
{
cout<<"\nIl numero "<<num1<<" e' negative";
}
break;
case 2:
break;
}
} while (s!=3);
return 0;
}
The other issue is: your function bool sepos(int numb) is declared to return a boolean value, you don't have to cast it.
numb>=0 returns true or false, you don't have to return those values explicitly.
bool sepos(int numb)
{
return numb>=0;
}
My Advice: Try to learn a bit more about the syntax of the language, you can avoid a lot of mistakes.
Related
My code prompts the user to input 0 or 1 as an integer in answer to one of the questions. I want the user to type Y or N. I tried to create a char variable, but I am not getting it right. It says y and n is not declared. I know it's a basic question, but I have just started learning c++.
Here is my code and below that the prompts, inputs, and output as well as a screenshot.
#include <iostream>
using namespace std;
int main() {
int a; // number of classes held
int b; // number of classed attended
int percent;
cout<<"Number of classes held "<<endl;
cin>>a;
cout<<"Number of classes attended "<<endl;
cin>>b;
percent = (b*100)/a;
if (percent>75 && percent<=100) {
cout<<"Congratulation you are allowed to sit in the examination your attendence is "<<percent<<"%";
} else if (percent<75) {
int m;
cout<<"Do you have any medical cause? (Respond in '1' for yes or '0' for no) "<<endl;
cin>>m;
if (m==1) {
cout<<"You are allwed due to a medical cause your percentage is "<<percent<<"%";
} else if (m==0) {
cout<<"You are not allowed to sit in the examination your percentage is "<<percent<<"%";
} else if (m!=1 && m!=0) {
cout<<"Invalid Responce";
}
} else {
cout<<"invalid attendence";
}
return 0;
}
cout<<"Number of classes held "<<endl;
cin>>a;
cout<<"Number of classes attended "<<endl;
cin>>b;
percent = (b*100)/a;
if (percent>75 && percent<=100) {
cout<<"Congratulation you are allowed to sit in the examination your attendence is "<<percent<<"%";
} else if (percent<75) {
int m;
cout<<"Do you have any medical cause? (Respond in '1' for yes or '0' for no) "<<endl;
cin>>m;
if (m==1) {
cout<<"You are allwed due to a medical cause your percentage is "<<percent<<"%";
} else if (m==0) {
cout<<"You are not allowed to sit in the examination your percentage is "<<percent<<"%";
} else if (m!=1 && m!=0) {
cout<<"Invalid Responce";
}
} else {
cout<<"invalid attendence";
}
return 0;
}
Here is the output of my code:
Number of classes held
100
Number of classes attended
53
Do you have any medical cause? (Respond in '1' for yes or '0' for no)
1
You are allwed due to medical cause your percentage is 53%
screenshot of code and output
Try using char:
char m;
std::cin >> m;
if (m == 'y')
// do something
else if (m == 'n')
// do something else
I think I understood the problem. To do this, you can also include the string library with #include <string>. So you can enter the "y" and "n" values you want as strings.
I leave a sample code for you.
#include <iostream>
#include <string>
using namespace std;
int main() {
string answer;
cout << "yes or no=? ";
cin >> answer;
if(answer == "y")
{
cout << "YES!";
}
else if(answer == "n")
{
cout << "NO!";
}
else {
cout << "TRY AGAIN!";
}
return 0;
}
New to Stack and C++.
I want to terminate a loop that regurgitates numbers fed to it with a character. Say, Q for Quit. The following program is functional and free of syntax errors. How can I terminate this loop without editing the input parameter?
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool run = true;
while(run)
{
cout<<"Enter your two favorite numbers."<<endl;
int num1;
int num2;
cin>>num1>>num2;
cout<<"You entered "<<num1<<" and "<<num2<<"."<<endl;
}
return 0;
}
You want the break statement. But you'll also then need to read a string for the first input, rather than an integer.
while(run)
{
cout<<"Enter your two favorite numbers, or 'Q' to exit."<<endl;
string input;
int num1;
int num2;
cin>>input1;
if (input == "Q")
{
break;
}
else
{
num1 = stoi(input);
}
cin>>num2;
cout<<"You entered "<<num1<<" and "<<num2<<"."<<endl;
}
scratching my head on this as it was working just fine earlier but when I went to add some other functions suddenly my program freaked out and I can not get it back to what it was.
class has me writing a rock/paper/scissors program to go up against a computer, any help with why the loop keeps terminating itself would be wonderful
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void RPSout(char);
int RPScomp();
int main() {
char choice;
int endit=0;
while (endit == 0)
{
cout << "\n\n\tReady to play Rock/Paper/Scissors against the computer??(please choose R/P/S)(Q to quit)\n";
cin >> choice;
RPSout(choice);
if (choice=='Q'||'q')
{endit=1;}
}
return 0;
}
void RPSout(char choose)
{
int RPS =0;
int comp=0;
switch (choose)
{
case 'R':
case 'r':
{
cout <<"Your choice: Rock";
break;
}
case 'P':
case 'p':
{
cout <<"Your choice: Paper";
break;
}
case 'S':
case 's':
{
cout << "Your choice: Scissors";
break;
}
case 'Q':
case 'q':
{
cout << "Bye Bye Bye";
break;
}
default:
cout<<"You enter nothing!"<<endl;
cout << "The valid choices are R/P/S/Q)";
}
return;
}
int RPScomp()
{
int comp=0;
const int MIN_VALUE =1;
const int MAX_VALUE =3;
unsigned seed = time(0);
srand(seed);
comp =(rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE;
return comp;
}
if (choice=='Q'||'q')
This is equivalent to
if ((choice == 'Q') || 'q')
Which is almost certainly not what you want. 'q' is a non-zero char literal, which is "truthy" and so this expression will never be false. It's akin to writing if (choice == 'Q' || true).
The solution is:
if (choice=='Q' || choice=='q')
The statement
if (choice=='Q'||'q')
always tests true and therefore sets your flag to terminate the loop.
Try:
if (choice=='Q'||choice=='q')
I think your if statement should be if (choice=='Q'|| choice=='q')
Your issue if with the if statement
if (choice=='Q'||'q')
{endit=1;}
the || 'q' part will always be true since 'q' in ASCII is not 0
Change your code to
if (choice=='Q'|| choice=='q')
{endit=1;}
I was writing a simple calculator in c,whereby the user inputs two numbers and afterward choose the operation to apply on them.(Mul,Add,Div,Sub)The program works except it skips the part where its supposed to take user input for the operand.what am i doing wrong
#include<stdio.h>
#include<sstream>
#include<iostream>
#include<conio.h>
#include<string.h>
using std::cout;
using std::cin;
using namespace std;
int main() {
char Operative[100];
int a;
int b;
int c;
printf("Enter First Number\n");
scanf("%d",&a);
printf("Enter First Number\n");
scanf("%d",&b);
printf("\nPlease Enter Operation(M,A,D,S)");
gets(Operative);
//getline(cin,Operative);
if (Operative == "M")
{
c = a*b;
printf("Multiplication value is %d",c);
}
else if (Operative == "A")
{
c = a+b;
printf("Addition value is %d",c);
}
else if (Operative == "D")
{
c = a/b;
printf("Division value is %d",c);
}
else if (Operative == "S")
{
c = a-b;
printf("\nSubtraction value is %d",c);
}
}
Use strcmp to compare strings values. == compares pointers:
if (strcmp (Operative, "M") == 0) ...
You have to call cin.ignore() (after scanf, before gets) first, because there is still \0 in the buffer.
You can also use switch statement.
switch(operation){
case 'P':
result = a+b;
printf("%d + %d = %d", a,b,result);
.
.
.
Just code this in your int main() if you prefer to use printf and scanf you replace and everything will be ok.
do {
cout << "Option 1 <<endl;
cout << "Option 2 <<endl;
cout << "Option 3 <<endl;
cin >> x;
switch (x) {
case 1:
//Your operation here
break;
case 2:
//Your operation here
break;
case 3:
//Your operation here
break;
case 4:
exit(0);
break;
default:
cout << "Input a valid option" <<endl;
}
} while(x);
I am instructed that I have to reject any decimal and I need to re enter the number again.I tried this code but still it just goes to the whole process before acknowledging the error. Try the program and judge me :D here's my code:
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<limits>
using namespace std;
int getInt()
{
int m=0;
while (!(cin >> m))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Please input a proper 'whole' number: " ;
}
return (m);
}
int main()
{
double x;
int q,w,e,choice;
cout<<"Welcome! This program will sort out the integers you will input!\nPlease input number of integers: ";
cin>>q;
cout<<endl<<endl;
int* inc= new int[q];
int* dec= new int[q];
for(int p=1;p<=q;++p)
{
w=p;
e=p;
cout<<"Input integer number "<<p<<": ";
x =getInt();
while(e>0 && inc[e-1]>x)
{
inc[e]=inc[e-1];
e--;
}
while(w>0 && dec[w-1]<x)
{
dec[w]=dec[w-1];
w--;
}
inc[e]=x;
dec[w]=x;
}
cout<<endl;
cout<<"What order do you prefer? Input 1 for increasing and 2 if decreasing.\nChoice: ";
cin>>choice;
while(choice<1 || choice>2)
{
cout<<"Please input a correct choice! Try again!\nChoice: ";
cin>>choice;
}
if(choice==1)
{
for(int i=0;i<q;++i)
cout<<inc[i]<<"\t";
cout<<endl;
}
else
{
for(int i=1;i<=q;++i)
cout<<dec[i]<<"\t";
cout<<endl;
}
system("PAUSE");
}
hoping for your help :)
You can try using modulo.
Just an idea, hope it helps.
bool flag = false;
While (flag == false){
cin>>number;
if ((number % 1) != 0){
flag = false;
}
else{
flag = true;
}
cin.clear();
}
Try making a copy of the number you want to test and casting it to an int and then back to a double, and then check for equality. If they are equal, you have an int, if they are not, you have a decimal:
#include <iostream>
using namespace std;
int main()
{
double a = 5;
double c = 5.5;
double b = a;
bool test1 = (double)((int)b) == a; //true!
b = c;
bool test2 = (double)((int)b) == c; //false!
cout << test1 << endl;
cout << test2 << endl;
return 0;
}
Wrote this answer a long time ago, it is very hacky and will not work on all inputs. Use std::stoi and check if it throws as the comment suggests instead.