I want to declare a variable inside an "if" statement and do something about the variable after the "if" statement. Here is my simplified code:
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if (a==1)
string b;
else
int b;
cin >> b;
//and some long codes using variable b
}
Is it possible to do this? What should I do to declare a variable with different data types(under different conditions) by using the same variable name?
No.
You cannot have a global scope for the variable you declare inside any local code block. The variables are local to the block it is declared in.
But you can think of using functions for "long_code_block_using_b" that you call inside the same block as the variable being declared
again if you do the same thing for all data types you can use function templates as below.
#include <iostream>
using namespace std;
template <typename T>
T long_code_using_variable_b()
{
T b;
cin >> b;
//and some long codes using variable b
return b;
}
int main() {
int a;
cin >> a;
if (a==1)
cout << long_code_using_variable_b<string>();
else
cout << long_code_using_variable_b<int>();
}
Related
in the following code for swapping by call by refernce is used
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& c,int& d)
{
int temp;
temp=c;
c=d;
d=temp;
}
int main()
{
int a,b;
cout<<" Enter value of a";
cin>>a;
cout<<"\n Enter value of b";
cin>>b;
swap(a,b);
cout<<"a: "<<a;
cout<<"\n b:"<<b;
return 0;
}
but if i use class and make object then there is no need of '&' refernce in calling the method.
#include <iostream>
#include <conio.h>
using namespace std;
class swapy
{
public:
int a;
int b;
void swap(int c,int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
};
int main()
{
swapy s;
cout<<" Enter value of a";
cin>>s.a;
cout<<"\n Enter value of b";
cin>>s.b;
swap(s.a,s.b);
cout<<"a: "<<s.a;
cout<<"\n b:"<<s.b;
return 0;
}
Can you explain how this worked?Why & was not needed in the second program.
First of all you are not calling your class swap() function, because you are not calling it with your class object s, for class swap() function you have to call it with s. like
s.swap(c,d);
Further Change your function swap() name to any other name like mySwap() then you'll get to know that this is not correct, because swap() is a built-in function of std::
so it will be simple to understand if you change the name of your function.
Furthermore, you are not using class member variables correctly, you have to do something with your class member variables a and b. your class is useless here.
I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}
Why wouldn't the following code compile? Basically what is not right in the following code? I'm assuming that declaring the same variable twice without assigning any value would be the problem.
#include <iostream>
using namespace std; int foo() { return 1; }
int main() { int a; int a; cout << foo() << endl; return 0;}
remove one "int a;" declaration. Even if it was possible, there is no reason to do that.
I keep getting an error message in CodeBlocks it says:
Error: 'Addnumbers' was not declared in this scope
I just started with C++ and have no idea what this means. Here is my code:
#include <iostream>
using namespace std;
int main()
{
int fnum;
int snum;
cout << "Enter First number" << endl;
cin >> fnum;
cout << "Enter Second Number" << endl;
cin >> snum;
Addnumbers (fnum, snum);
return 0;
}
int Addnumbers(int fnum, int snum){
int ans = fnum+snum;
return ans;
}
You need to declare the function before it's used:
int Addnumbers(int fnum, int snum);
int main()
{
}
int Addnumbers(int fnum, int snum)
{
// ...
}
The first declaration is what is called a prototype, and tells the compiler that somewhere there is a function named AddNumbers with the specified arguments and return type. Then you can have the definition anywhere, even in another source file.
In C++ (as well as in C or other languages base on C) everything must be declared before it it used. That's how the compiler will know that stuff exists.
You need to either move Addnumbers before main, or to do a forward declaration:
#include <iostream>
using namespace std;
int Addnumbers(int fnum, int snum);
int main()
{
I have a quick question about namespace scope:
I have two namespaces, A and B, where B is nested inside A.
I declare some typedefs inside A.
I declare a class inside B ( which is inside A )
To access the typedefs (declared in A), from inside B, do I need to do "using namespace A;"
ie:
B.hpp:
using namespace A;
namespace A {
namespace B {
class MyClass {
typedeffed_int_from_A a;
};
}
}
This seems redundant... Is this correct?
To access the typedefs (declared in A), from inside B, do I need to do "using namespace A;"
No.
However if there is a typedef or some other symbol with same name as your typedef, defined in the namespace B, then you need to write this:
A::some_type a;
Lets do a simple experiment to understand this.
Consider this code: (must read the comments)
namespace A
{
typedef int some_type; //some_type is int
namespace B
{
typedef char* some_type; //here some_type is char*
struct X
{
some_type m; //what is the type of m? char* or int?
A::some_type n; //what is the type of n? char* or int?
};
void f(int) { cout << "f(int)" << endl; }
void f(char*) { cout << "f(char*)" << endl; }
}
}
int main() {
A::B::X x;
A::B::f(x.m);
A::B::f(x.n);
return 0;
}
Output:
f(char*)
f(int)
That proves that type of m is char* and type of n is int as expected or intended.
Online Demo : http://ideone.com/abXc8
No, you don't need a using directive; as B is nested inside of A, the contents of A are in scope when inside B.