I'm trying to write a very simple program in C++ that finds the modulus of two numbers as follows:
#include <iostream>
using namespace std;
int n;
int d;
int modulus;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
cin>>d;
modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}
But, when I try to compile it, I get the following:
How can this be solved?
Thanks.
You get the error because the name of your global variable modulus clashes with std::modulus. To fix this, you can:
Make modulus a local variable
Rename the modulus variable
Remove using namespace std and either import the names you need from std individually or qualify them with std::
Because you have using namespace std; it clashes with std::modulus
Corrected version:
#include <iostream>
using std::cout;
using std::cin;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
int n;
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
int d;
cin>>d;
int modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}
Related
Is it possible display an input message as follows?
Enter First Fraction:_/_
Enter Second Fraction: _/_
Where _ are Input spaces?
Using some sort of the following code??
cout<<"Enter First Fraction: ";
cin>>N1>>"/">>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>"/">>D2;
OR
cout<<"Enter First Fraction: ";
cin>>N1>>/>>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>/>>D2;
Here is the Solution to my problem just in case anyone else is facing it..
Credits go to #qPCR4vir
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
main()
{ //This program encourages the user to perform a sum of two fractions.
int N1, D1, N2, D2, N, D;
char divide{};
system("cls");
cout<<"The Format is: 'A/B' & 'C/D'..\n\n";
cout<<"Enter First Fraction: ";
cin>>N1>>divide>>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>divide>>D2;
if (divide=='/')
{
N=(N1*D2)+(D1*N2); //Numerator
D=D1*D2; //Denominator
cout<<"Sum of Both Fractions is: "<<N<<"/"<<D;
}
else
{
system("cls");
cout<<"The Correct Format is: A/B & C/D\nWhere these alphabets are Integers..\n\n";
cout<<"Example: 4/5";
}
getch();
system("cls");
return(0);
}
Here's the Part of code that Specifies the Format ONLY for 'cin' Statement.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
char divide{}; //iota{};
int x{},y{};
cout<<"Enter Dividion of Two Numbers (A/B): ";
cin>>x>>divide>>y; //>> iota;
if (divide=='/') //&& iota=='i')
{
//x=(N1*D2)+(D1*N2); //Numerator
//y=D1*D2; //Denominator
cout<<"The Fractional Form is: "<<x<<"/"<<y;
}
else
{
system("cls");
cout<<"The Correct Format is: A/B & C/D\nWhere these alphabets are Integers..\n\n";
cout<<"Example: 4/5";
}
getch();
return 0;
}
Note: This is Simplified/Modified Solution of #qPCR4vir at; Reading in a specific format with cin
I have this code which is giving me error, I have no idea how to fix it.
I don't know which part I'm doing wrong.
I have made a code like this but with out giving user a option,
for example:
int counter;
for (counter=1; counter<=100000; counter=counter+1)
{
cout<<"2x"<<counter<<"="<<2*counter<<"\n";
this code is giving me a table of 2.
#include <iostream>
#include <cstdlib>
using namespace std;
main()
{ int counter, number, maxMultiplier;
{
cout>> "Please enter the number for which you want a table: ";
cin<< number;
cout>> "Please select the multiplier up to which you want a table: ";
cin>> maxMultiplier;
}
for (counter=1; counter<=maxMultiplier; counter=counter+1)
{ cout<<number<<"x"<<counter<<"="<<number*counter<<"/n";
}
{system("pause");}
return main();
}
The user should be able to enter the number for table they want and how long they wants a table to be. Like all the way up to 2x1=2.........2x10=20
after cleaning the code and taken into account the remarks of TypeIA and me :
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int number, maxMultiplier;
cout << "Please enter the number for which you want a table: ";
cin >> number;
cout << "Please select the multiplier up to which you want a table: ";
cin >> maxMultiplier;
for (int counter = 1; counter<=maxMultiplier; ++counter)
{
cout<<number<<"x"<<counter<<"="<<number*counter<<"\n";
}
system("pause"); // I don't like, pause does not exist under Linux etc
return 0;
}
I am new in programming I am studying about for loop I tried it in various ways and output every time was different.
Code when I wrote only "a" to initialize for loop:
input was "Enter binary number:11011"
Output was "Decimal Number=27"
#include <iostream>
using namespace std;
int main(){
int a,b,c=1,d=0;
cout<<"Enter binary number:";
cin>>a;
for (a;a!=0;a=a/10) {
b=a%10;
d=d+b*c;
c=c*2;
}
cout<<"Decimal Number="<<d<<endl;
}
Code when I wrote "int a" to initialize for loop:
input was "Enter binary number:11011"
Output was "Decimal Number=0"
#include <iostream>
using namespace std;
int main(){
int a,b,c=1,d=0;
cout<<"Enter binary number:";
cin>>a;
for (int a;a!=0;a=a/10) {
b=a%10;
d=d+b*c;
c=c*2;
}
cout<<"Decimal Number="<<d<<endl;
}
Code when I wrote nothing to initialize for loop:
input was "Enter binary number:11011"
Output was "Decimal Number=27"
#include <iostream>
using namespace std;
int main(){
int a,b,c=1,d=0;
cout<<"Enter binary number:";
cin>>a;
for (;a!=0;a=a/10) {
b=a%10;
d=d+b*c;
c=c*2;
}
cout<<"Decimal Number="<<d<<endl;
}
Your first and last code snippets do not differ from each other, apart from a being evaluated and its value discarded in the initialization section of the first for loop
Your second loop re-declares a without initializing it. Variable a inside the for loop is not the same as the variable a outside the loop, so reading its uninitialized value is undefined behavior.
Since you do not really need a separate loop variable, using a while loop is a more common choice:
while (a) {
b = a%10;
d += b*c;
c *= 2;
a /= 10;
}
Note the use of compound assignment expressions +=, *=, and /=. A compound assignment of the form x += y is equivalent to x = x + y
I want to create number of structure objects using input from user
for example:
I want to accept user value n and create n number of objects and pass these objects to a function where I initialize the variables to them.
#include <iostream>
#include<string>
#include "stdio.h"
using namespace std;
struct student
{
int roll_no;
char name[20];
};
void get_input(student p[],int n1)
{
for(int i=1;i<=n1;i++)
{
cout<<"Enter Roll Number ";
cin>>p[i].roll_no;
cout<<"\n Enter Name of the student: ";
cin>>p[i].name;
}
}
int main()
{
int n;
cout<<"How many student details would you want to enter: ";
cin>>n;
//Want to create number of object based on input n
student p[n];
get_input(student p[],n);
return 0;
}
There are a number of problems with your example.
The first problem is student p[n];. This is not strictly valid c++. Some compilers allow it as an extension. Without knowing which compiler you are using, and with what flags, I'll assume this is part of the problem. The typical solution for this problem is to use std::vector. An std::vector works in many ways like an array of variable size. std::vector<student> p(n); will create a vector named p containing n default constructed student objects.
The next problem is get_input(student p[],n);. It's unnecessary and incorrect to name the type when passing an argument. Just write get_input(p,n);. After all, you didn't specify that n is int when you called get_input. However, since p is an std::vector now, we need to add .data() to fetch a pointer to the actual data. It becomes get_input(p.data(), n);.
The final critical issue is the loop for (int i = 1; i <= n1; i++). Imagine n is 3. The values i will take are 1, 2 and 3. However, arrays are indexed starting at 0. If n is 3, you want to access the elements 0, 1 and 2. The correct loop is for (int i = 0; i < n1; i++).
These changes will allow your example to work but there are still many improvements that can be made.
#include <iostream>
#include <vector>
using namespace std;
struct student
{
int roll_no;
char name[20];
};
void get_input(student p[], int n1)
{
for (int i = 0; i < n1; i++)
{
cout << "Enter Roll Number ";
cin >> p[i].roll_no;
cout << "\n Enter Name of the student: ";
cin >> p[i].name;
}
}
int main()
{
int n;
cout << "How many student details would you want to enter: ";
cin >> n;
//Want to create number of object based on input n
std::vector<student> p(n);
get_input(p.data(), n);
return 0;
}
Consider using std::string instead of char name[20]. You won't have to guess how long a name might be, and you don't risk undefined behavior from having longer names.
struct student
{
int roll_no;
std::string name;
};
Consider passing p by reference, instead of using a pointer and size.
// Declaration / definition
void get_input(std::vector<student> & p)
// Usage
get_input(p);
Consider using a ranged based for loop instead of a regular for loop.
void get_input(std::vector<student> & p)
{
// for each student in p
for (student & s : p)
{
cout << "Enter Roll Number ";
cin >> s.roll_no;
cout << "\n Enter Name of the student: ";
cin >> s.name;
}
}
Use a vector of student: Here is some example code of how you can do it:
#include <iostream>
#include <string>
#include <vector>
#include "stdio.h"
using namespace std;
struct student
{ int roll_no;
char name[20];
};
void get_input(vector<student> & p1, int n1)
{
for (int i=0; i<n1; i++)
{
student s;
cout<<"Enter Roll Number: ";
cin>>s.roll_no;
cout<<"\n Enter Name of the student: ";
cin>>s.name;
p1.push_back(s);
}
}
int main()
{
int n;
cout<<"How many student details would you want to enter: ";
cin>>n;
//Want to create number of object based on input n
vector<student> p;
get_input(p, n);
return 0;
}
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 8 years ago.
Improve this question
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
long int d2b(int);
static long int binary=0;
static int i=0;
long int d2b(int num)
{
if(num!=0)
{
binary=binary+pow(10,i)*(num%2);
d2b(num/2);
i++;
}
return(binary);
}
int main()
{
int num;
long int binary_ans=0;
cout<<"Enter the number.";
cin>>num;
binary_ans=d2b(num);
cout<<"Ans = "<<binary_ans;
getch();
return(0);
}
I am using Dev C++ compiler and this code doesnt seem to work. Can somebody please run this code on their compilers and give me a feedback. Also if the code seems incorrect to you, please tell me the reason why you think so.
A correct implementation with neither static variables nor globals (both are just plain evil, try to avoid them at any cost) that gives you the desired output can be something as simple as
#include <iostream>
long int d2b(int x)
{
return x ? (x%2 + 10*d2b(x/2)) : 0;
}
int main()
{
std::cout << "enter a number:" << std::endl;
int input;
std::cin >> input;
long int binary = d2b(input);
std::cout << "ans = " << binary << std::endl;
}
Take note that, as we're using int to store our binary representation, the whole thing breaks for any input > 524287.
This of course assumes your integer is 64bit.
The largest number you can write using only 0 and 1 is int x = 1111111111111111111, which when you interpret it as binary translates to 524287.
For 32bit and even 16bit integers this range is significantly lower.
Also have a look at: Are global variables bad?
Obviously i++ must be done before the recursion takes place.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
long int d2b(int);
static long int binary=0;
static int i=0;
long int d2b(int num)
{
if(num!=0)
{
binary=binary+pow(10,i)*(num%2);
i++;
d2b(num/2);
}
return(binary);
}
int main()
{
int num;
long int binary_ans=0;
cout<<"Enter the number.";
cin>>num;
binary_ans=d2b(num);
cout<<"Ans = "<<binary_ans;
_getch();
return(0);
}
Get used to debugging your code!
As mentioned earlier, you can use it also in this manner:
#include<iostream>
#include<math.h>
using namespace std;
string d2b(int);
static string binary;
string d2b(int num)
{
if(num!=0)
{
binary= to_string(num%2) + binary;
d2b(num/2);
}
return(binary);
}
int main()
{
int num;
string binary_ans="";
cout<<"Enter the number.";
cin>>num;
binary_ans=d2b(num);
cout<<"Ans = "<<binary_ans;
getch();
return(0);
}