int : redefinition (C++) - c++

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int x, y;
int main()
{
cout << "Please give me a number:";
int x = (cin, x);
cout << "Please give me another number:";
int y = (cin, y);
cout << "The sum of " << x;
cout << "and " << y;
cout << "is " << x+y;
}
Can anyone tell me why(as simple as it is) this doesn't add?
I'm not really sure how to return user input for numbers and the like. Just started learning this.

I believe instead of this:
int x = (cin, x);
you wanted this:
cin >> x;
cin (console input) works pretty much the same way as cout (console output), which you used properly.
You may want to read about them more:
std::cout
std::cin
Also, you do not need to redefine x and y in main(), as they are global variables.

Correct code is :
#include <iostream> // for cin,cout we use iostream
#include <stdio.h> // you don't need this header file in this program
#include <string> // also you don't need this header
using namespace std;
int main()
{
int x,y;
cout<<"Please give me a number : ";
cin>>x;
cout<<"Please give me another number : ";
cin>>y;
cout<<"The sum of "<<x<<" and "<< y<<" is "<<x+y;
return 0;
}
Read Basic_Syntax

Related

I'm trying to create a currency conversion program with c++, I don't know how to continue

#include <iostream>
using namespace std;
int Convert(int dollars)
{
int amount = dollars * 113;
cin >> dollars;
}
int main ()
{
Convert ();
count << "Enter the amount you want to convert";
}
There are multiple issues with your program, first it is expecting an argument which you are not supplying, move cin to main and send the input to Convert() function. Then, return the conversion and print it.
#include <iostream>
using namespace std;
int Convert(int dollars)
{
return (dollars * 113);
}
int main ()
{
int amount;
cout << "Enter the amount you want to convert and press enter ";
cin >> amount;
cout << "Result is = " << Convert(amount) << endl;
return 0;
}

Can two class members be used in the same function? (classes and objects) (C++)

I am new to programming and I have started with C++. At this point I am experimenting with classes and objects.
My problem is that I am not able to get the correct result in what I want.
Here is my code:
#include <iostream>
using namespace std;
class Variable
{
public:
int classAnum;
int classBnum;
void sumVar(){
cout << classAnum + classBnum <<endl;
}
};
int main()
{
Variable numA;
Variable numB;
cout << "Enter a number: ";
cin >> numA.classAnum;
cout << "Enter another number: ";
cin >> numB.classBnum;
numA,numB.sumVar();
return 0;
}
The output that I am being given is:
Enter a number: (any number)
Enrer another number: (any number)
1955294086
I get this output with any number I enter.
But if I use this code:
#include <iostream>
using namespace std;
class Variable
{
public:
int classAnum;
int classBnum;
void sumVar(){
cout << classAnum + classBnum <<endl;
}
};
int main()
{
Variable numA;
Variable numB;
cout << "Enter a number: ";
cin >> numA.classAnum;
cout << "Enter another number: ";
cin >> numB.classBnum;
cout << numA.classAnum + numB.classBnum;
return 0;
}
I get a correct result. Can someone explain me if I am able to use two objects in the same function? And if I can, how should it be?
If you'd like to input two numbers into Variable class instance and then call the function sumVar() to print the sum of the two numbers, you can do this:
#include <iostream>
using namespace std;
class Variable
{
public:
int classAnum;
int classBnum;
void sumVar(){
cout << classAnum + classBnum <<endl;
}
};
int main()
{
Variable num;
cout << "Enter a number: ";
cin >> num.classAnum;
cout << "Enter another number: ";
cin >> num.classBnum;
num.sumVar();
return 0;
}
Seeing your code, it looks like you want to find the sum of two numbers which are members of your class. you don't need to instantiate 2 objects for that, you can accomplish it by instantiating only one object, and initialize the properties this way:
#include <iostream>
using namespace std;
class Variable
{
public:
int number1;
int number2;
void sumVar(){
cout << number1 + number2 <<endl;
}
};
int main()
{
Variable object;
cout << "Enter a number: ";
cin >> object.number1;
cout << "Enter another number: ";
cin >> object.number2;
object.sumVar();
return 0;
}
This way you can easily sum up members of your class.
in the future, if you want to add two objects of your own classes, you may need to use operator overloading concept that you can find on several tutorial online.
Good Luck !

I need to make an exponential calculator, but it can only handle up to 20^15, how can It handle more?

//This is my code so far
//It is in microsoft studio visual
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
int main()
{
int a, b;
cout << "Input a whole number:"; //The following lines are to make it interactive
cin >> a;
cout << "Input a whole number:";
cin >> b;
int result = 1;
for (int i = 0; i < b; i++)
result *= a;
cout << "The Answer is:" << result;
cout << "\n";
system("PAUSE");
}
//How do I expand the power of it to handle more?
I just need to increase the power of it to handle more. I need this for a class and I can't quite figure it out. I met the qualifications, but I want it to be more powerful.

Is this a bug in "Code::Blocks" or I am doing something wrong

I made this simple c++ program in code::blocks IDE:
#include <iostream>
#include "funct.cpp"
using namespace std;
int main()
{
float a, b;
cout << "enter a : ";
cin >> a;
cout << "enter b : ";
cin >> b;
cout << "\n\nThe result is: " << funct(a, b) << "\n";
return 0;
}
and this function:
#include <iostream>
using namespace std;
float funct(float x, float y)
{
float z;
z=x/y;
return z;
}
when I create the function in the IDE by creating new empty file and try to build the program it returns this error:
but when I create the same function file manually by the text editor and put it in the same folder of the project it works fine and the compiler can build it with no errors.
So is this because I am doing something wrong or is it a bug in the IDE ?
Can you help me out of this ?
and thanks in advance.
You are messing up the project:
1st you should do is create a header file function.h or function.hpp, in there place the header of the function
function.h:
float funct(float x, float y);
then a
function.cpp: that is where the concrete implementation happens:
float funct(float x, float y)
{
float z;
z = x / y;
return z;
}
then you are ready to include that into another file:
#include <iostream>
#include "funt.h"
using namespace std;
int main()
{
float a, b;
cout << "enter a : ";
cin >> a;
cout << "enter b : ";
cin >> b;
cout << "\n\nThe result is: " << funct(a, b) << "\n";
return 0;
}
you can for sure see a dirty/not_good-practice version where there is no header
in that version no include is required, but you need to prototype the functions you need
function.cpp: that is where the concrete implementation happens:
float funct(float x, float y)
{
float z;
z = x / y;
return z;
}
and the main:
#include <iostream>
using namespace std;
float funct(float x, float y);
int main()
{
float a, b;
cout << "enter a : ";
cin >> a;
cout << "enter b : ";
cin >> b;
cout << "\n\nThe result is: " << funct(a, b) << "\n";
return 0;
}
as Neil Butterworth said above, no cpp files must be included..
Don't include .cpp files. Instead put a forward declaration of the function in a .h or .hpp file that would look like this float funct(float x, float y);, and include that file.

Do-While to ask user to repeat entire int main programme

Very new to programming and l can't find any basic explanation online or code that will work well for what l need. I have a fairly long piece of programme (approx 300 lines) it all works. This is the structure to give an idea:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//code....
{
//code... etc...
}
}
I want to ask the user to repeat the programme. If enters y, then repeat int main up to the point of asking the same repeat question again. Else to cout<< "e.g. Thank you, goodbye";
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
//using namespace std; <--- Don't use using namespace std, it pollutes the namespace
void repeat()
{
//... code to repeat
}
int main()
{
//code....
char answer;
while((std::cin >> answer) != 'y')
{
repeat();
}
}
#include <iostream>
#include <conio.h>
using namespace std;
//Class
class DollarToRs {
public:
int Dollar;
int Rs;
int ToRs;
ConversionToRs() {
cout << "Enter the amount of Dollar: ";
cin >> Dollar;
ToRs = Dollar * 154;
cout << "This is the total amount in PKR: " << ToRs <<endl;
}
};
int main()
{
//Dollar Convertion Function
DollarToRs convert;
convert.ConversionToRs();
//For Repeating Program
int repeat;
int exit;
cout << "To repeat program enter 1" <<endl;
cin >> repeat;
while (repeat == 1) {
convert.ConversionToRs();
cout << "To repeat program enter 1" <<endl;
cin >> repeat;
}
exit =0;
if (exit == 0) {
}
getch();
return 0;
}
Here's an example of a simple solution:
int main()
{
for (;;) // "infinite" loop (while (true) is also possible)
{
// stuff to be repeated here
cout << "Repeat? [y/n]" << endl;
char answer;
cin >> answer;
if (answer == 'n')
break; // exit loop
} // else repeat
cout << "Thank you, goodbye" << endl;
}
Here's another one:
int main()
{
bool repeat = true;
while (repeat)
{
// stuff to be repeated here
cout << "Repeat? [y/n]" << endl;
char answer;
cin >> answer;
repeat = answer == 'y';
}
cout << "Thank you, goodbye" << endl;
}
As a side note, don't do this: #include <stdlib.h>. In C++ you should use the c prefixed header file names when using the C headers: #include <cstdlib> and #include <ctime>.