C++ variable use

By ChunkieMonkey on Aug 26, 2008

Simple variable use in C++

//My compiler is dev c++
//Second snippet made

#include <iostream>

using namespace std;

int z;

int main(){

    int x;
    int y;

    cout << "Type a number. ";
    cin >> z;

    cout << "Type another. ";
    cin >> x;

    z = x*y;

    cout << "The two you entered multiplied is " << z << "\n";

    system("pause");

}

Comments

Sign in to comment.
[HANZam]   -  Aug 04, 2009

@ChunkieMonkey

in my opinion your declaring Z as your 1st number and X as 2nd number but your condition said Z=X*Y.. better change your inputs very well..that's all.

and for you Zmodem

include

using namespace std;

int main()
{
int num1,num2,product;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
product = num1 * num2;
cout << "The product is: " << product << endl;
system("pause");
return 0;
}

better change it dude..

int num1,num2,product;>>> to double num1,num2,product;

cause integers are declaring only a single variable..

 Respond  
napalm`   -  Sep 15, 2008

lol @ him saying this actually works. need to rearrange those variables.

 Respond  
Zmodem   -  Sep 15, 2008

DiGiTaL: Good point. Get rid of those nasty leftovers and ignore that enter key.

 Respond  
DiGiTaL   -  Sep 15, 2008
   cout<<"The product is"<<num1*num2<<endl;
   cin.ignore();
 Respond  
Zmodem   -  Sep 15, 2008

Just like to toss in my two cents, so bear with me.

A major contradiction in programming is using meaningful variable names, which, in this case, make no sense at all. Why not use names like: num1, num2 & product? Anyways, I'm using Visual Studio 2008 and when I compile this code, I always get a product response of:

The two you entered multiplied is 0

No matter what numbers I've entered, the product is always 0. The reason for this is quite obvious, as everyone has already stated that your variables are all mixed up and in the wrong spot. In any case, something to the correct deter would be:

#include <iostream>
using namespace std;

int main()
{
  int num1,num2,product;
  cout << "Enter first number: ";
  cin >> num1;
  cout << "Enter second number: ";
  cin >> num2;
  product = num1 * num2;
  cout << "The product is: " << product << endl;
  system("pause");
  return 0;
}
 Respond  
DiGiTaL   -  Sep 15, 2008

Variable scoping problem here,

Why make x,y & z a global variable when you only need it within the function. You might as well do this in C not C++.

 Respond  
F*U*R*B*Y*   -  Aug 30, 2008
//My compiler is dev c++
//Second snippet made

#include <iostream>
using namespace std;
int x,y,z;
int main(){
    cout << "Type a number. ";
    cin >> z;
    cout << "Type another. ";
    cin >> x;
    z = x*y;
    cout << "The two you entered multiplied is " << z << "\n";
    system("pause");
}

Cleaner, Easier, Nicer

 Respond  
Hawkee   -  Aug 26, 2008

Chunkie the problem is you aren't requesting y from the user, you're requesting x and z.

 Respond  
ChunkieMonkey   -  Aug 26, 2008

Actually it does work, I tested it on my compiler before I uploaded it. if you ran it through a compiler it asks you for a number, then another. The user of the script sets the values of the variable x and y.

 Respond  
EL   -  Aug 26, 2008

ChunkieMonkey
Comments: 103

C++ Snippet: C++ variable use
Posted on Aug 26, 2008 12:31 am
Hello people lookin at nemisis' annoying comment script. and this is a simple variable use w/e

Annouying huh so ignore me or haltdef it;p

 Respond  
KronicDreamer   -  Aug 26, 2008

yea should be y =z*x;

 Respond  
F*U*R*B*Y*   -  Aug 26, 2008

uh,

    cout << "Type a number. ";
    cin >> z;

    cout << "Type another. ";
    cin >> x;

    z = x*y;

that's just going to do

z = x*

as y isn't being set

 Respond  
ChunkieMonkey   -  Aug 26, 2008

Hello people lookin at nemisis' annoying comment script. and this is a simple variable use w/e

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.