-
Please Help w/Arrays
Hi! I need some help with a VERY simple program I'm making. It's a calulator that adds two numbers and uses arrays. I've just been doing this for a few days, so don't flame on me. This is the code:
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int x[3];
x[2]=x[0]+x[1];
int main(int argc, char *argv[])
{
//Prints instructions
cout << "This is a simple calculator. All it does" << endl;
<< "is add two numbers." << endl << "Please insert "
<< "your first number, press Enter, " << endl
<< "then the second and Enter."
//Asks for the first number and to press Enter
cin >> x[0] >> getch();
//Displays the plus sign
cout << " + ";
//Asks for the second number and to press Enter
cin >> x[1] >> getch();
//Displays the equal sign and the answer
cout << " = " x[2] << endl
//Prints instructions
<< "Please press Enter to exit this program.";
//Waits until user presses Enter
getch();
//Closes the program
return 0;
}
What's wrong? Could someone please help me? Happy holidays!
-scary_freak_x
-
_B-L-U-E_
Why would you think that you would get flamed, first off....?
I didn't look through everything, but your global declarations at the top will not work. You need to assign something to the array positions before you perform a calculation on it.
int x[3];
x[2] = 3;
x[1] = 2;
x[0] = x[2] + x[1];
or something to that effect.
Although... that happens before you even get to assign the numbers. Move your formula into the program for use after you grab the numbers and it will work....
-
_B-L-U-E_
# include<iostream>
using namespace std;
int main()
{
int x[3];
cout << "Number one: ";
cin >> x[0];
cout << "Number two: ";
cin >> x[1];
x[2] = x[0] + x[1];
cout << "\n\n" << x[0] << " + " << x[1] << " = " << x[2] << endl;
return 0;
}
-
Registered User
ROFL
Well, don't consider this a flame = )
The answer's pretty simple, you're not doing the calculations at the right time. You need to put the equation x[2] = (x[0] + x[1]); after you get both values, not at the beginning of the program where the values have no meaning really. = D
Popular pages
Recent additions