#include %26lt;fstream%26gt;
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
class fraction
public :
{ int num;
int den;
fraction ();
fraction (int n, int d);
fraction add (fraction f2);
fraction mult (fraction f2);
bool equal (fraction f2);
};
fraction fraction:: add(fraction f2)
bool fraction:equal (fraction f2)
(num*f2.den)+(den*fraction)+(den*f2.de...
{fraction temp;
temp.num =(num*f2.den)+(den*f2.den);
temp.den = (den*f2.den)
return temp;
}
{
return =(num* f2.den== den*f2.num);
}
void main ()
{
ofstream out;
out.open("output.txt");
fraction f1 (1,3) , f2 (1,2, f3);
f3=f1.add(f2);
cout %26lt;%26lt; "the sum is "%26lt;%26lt;f3.num%26lt;%26lt;"/"%26lt;%26lt;f3.den%26lt;%26lt;endl;
if (f1.equal (f2)
cout %26lt;%26lt; "the fractions are equal"%26lt;%26lt;endl;
else cout %26lt;%26lt; "the fraction are not equal"%26lt;%26lt;endl;
int n, d;
cout %26lt;%26lt;"Imput the first fraction"%26lt;%26lt;endl;
cin%26gt;%26gt;n%26gt;%26gt;d;
fraction f1 (n,d)
out.close();
}
i need to get it to output the imputed fractions and the answers
and yet i cant figure out that code %26amp; it wont work neways
C++ program problems once again...?
Well, the first thing I noticed is that you've only defined two constructors, including the default and one that takes two int arguments, but you're declaring f2 with three arguments. I think you put a parenthesis in the wrong place.
You're missing a parenthesis in the initial decision structure:
if (f1.equal(f2))
You never actually use the output stream out.
You're missing semicolons on a number of lines.
All in all, I doubt this will compile. Is that what you mean when you say "it won't work"? Or is that just an artifact of transferring the code to YA? If you've gotten it to compile, it would help to know what the program does that you don't want it to do, or what it fails to do that you want it to.
Edit: Well, luv, the only suggestion I have is to fix the compile errors. I assume you're using something like VS or Borland or some other commercial package. You'd do well to read the error messages in some detail and learn exactly what they mean. I've noticed that beginners often just read them as "There's an error someplace" without learning your IDE's way of communicating exactly what error it found. If it's pointing out an error and you don't understand what it's saying, refer to the compiler documentation. With VS, at least, it's not unusal for the short error message to be cryptic at best. Usually the more detailed explanations in the compiler documentation are a lot more clear.
I'd also suggest that, initially at least, you stick to output to the default display with cout. Once you have the output to your liking, you can go back and add in writing the output to a file. When I was learning to handle file I/O, I found it useful to send the output to cout first. When it looked good, I added lines to send the output to a file. When what I got in the file looked like what I was getting on the screen, I went back and took out the cout lines.
Edit2: Jorge is right in that overloading the operators would give you much cleaner, easier code. However, I assumed you hadn't covered that as yet, as what you're doing is the usual dodge beginners use before they've learned the joys of overloading. Here, I'd suggest you continue with the code you have until you have it working. If you're supposed to overload things like the equality operator, you can add that in later on. Small steps are usually best for learning a language.
Edit3: Well, I was going to email you since these edits are getting tiresome, but you don't allow emails. I'm still not clear about whether you need to overload the operators or not, but my recommendation from earlier stands. Get it working with the functions you're using, THEN overload the operators. I think this is particularly important when you're trying to overload the insertion operator, %26lt;%26lt;. That one (and the extraction operator) are a bit harder to do than the others.
I'm also still unclear if your problems are compile errors, logic errors, or simple bugs. Feel free to email me to continue this discussion - preferable before we get a violation notice from Yahoo for "chatting".
Saturday, May 22, 2010
How do you use pointers with two dimensional arrays in C++?
So I have this program that deals with one dimensional arrays. I want to expand it to two with array[row][col] how can I do this?
#include %26lt;iostream%26gt;
using namespace std;
int* getArray(int row);
void printArray(int* array, int row);
int main()
{
int row;
cout %26lt;%26lt;"Enter row length: ";
cin%26gt;%26gt;row;
int* array = getArray(row);
cout %26lt;%26lt; "array in main" %26lt;%26lt; endl;
printArray(array, row);
/*int* array2 = getArray();
cout %26lt;%26lt; "array2 in main" %26lt;%26lt; endl;
printArray(array2);
cout %26lt;%26lt; "array in main" %26lt;%26lt; endl;
printArray(array);
delete[] array2;*/
delete[] array;
return 0;
}
int* getArray(int row){
int *array = new int[row];
for ( int i = 0; i %26lt; row; i++ ){
cout %26lt;%26lt; "Enter an integer: ";
cin %26gt;%26gt; array[i];
}
cout %26lt;%26lt; "array in getArray()" %26lt;%26lt; endl;
printArray(array, row);
return array;
}
void printArray(int* array, int row)
{
for ( int i = 0; i %26lt; row; i++ ){
cout %26lt;%26lt; *(array+i) %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
cout %26lt;%26lt; endl;
}
How do you use pointers with two dimensional arrays in C++?
Hi. I would love to help you. But, answering the question will be as long as your question. I thought that instead of giving a long answer, i could redirect you to some place where you can find good references.
http://www.cplusplus.com/doc/tutorial/
www.cplusplus.com/doc/tutorial/pointer...
www.codersource.net/c++_pointers.html
If you are still unable to find any useful information, and if there is anything specific that you would like to know, just drop in a mail to me. I shall try to help you.
Thanks! :)
Reply:u r goin right way baby
Reply:Generally speaking to access an array you use a for loop. This is for a 1 dimentional array. You want a two dimentional array.
So the way to do this is a nested for loop
ie.
for ( int i = 0; i %26lt; row; i++ ){
for ( int j = 0; j %26lt; col, j++ ){
cout %26lt;%26lt; "Enter an integer: ";
cin %26gt;%26gt; array[i][j];
}
}
That is all there is to it. You already understand the array concept. Now go ahead and extend what you know.
Good luck.
snow of june
#include %26lt;iostream%26gt;
using namespace std;
int* getArray(int row);
void printArray(int* array, int row);
int main()
{
int row;
cout %26lt;%26lt;"Enter row length: ";
cin%26gt;%26gt;row;
int* array = getArray(row);
cout %26lt;%26lt; "array in main" %26lt;%26lt; endl;
printArray(array, row);
/*int* array2 = getArray();
cout %26lt;%26lt; "array2 in main" %26lt;%26lt; endl;
printArray(array2);
cout %26lt;%26lt; "array in main" %26lt;%26lt; endl;
printArray(array);
delete[] array2;*/
delete[] array;
return 0;
}
int* getArray(int row){
int *array = new int[row];
for ( int i = 0; i %26lt; row; i++ ){
cout %26lt;%26lt; "Enter an integer: ";
cin %26gt;%26gt; array[i];
}
cout %26lt;%26lt; "array in getArray()" %26lt;%26lt; endl;
printArray(array, row);
return array;
}
void printArray(int* array, int row)
{
for ( int i = 0; i %26lt; row; i++ ){
cout %26lt;%26lt; *(array+i) %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
cout %26lt;%26lt; endl;
}
How do you use pointers with two dimensional arrays in C++?
Hi. I would love to help you. But, answering the question will be as long as your question. I thought that instead of giving a long answer, i could redirect you to some place where you can find good references.
http://www.cplusplus.com/doc/tutorial/
www.cplusplus.com/doc/tutorial/pointer...
www.codersource.net/c++_pointers.html
If you are still unable to find any useful information, and if there is anything specific that you would like to know, just drop in a mail to me. I shall try to help you.
Thanks! :)
Reply:u r goin right way baby
Reply:Generally speaking to access an array you use a for loop. This is for a 1 dimentional array. You want a two dimentional array.
So the way to do this is a nested for loop
ie.
for ( int i = 0; i %26lt; row; i++ ){
for ( int j = 0; j %26lt; col, j++ ){
cout %26lt;%26lt; "Enter an integer: ";
cin %26gt;%26gt; array[i][j];
}
}
That is all there is to it. You already understand the array concept. Now go ahead and extend what you know.
Good luck.
snow of june
C++ array?
#include %26lt;string%26gt;
#include %26lt;iostream%26gt;
using std::cout;
using std::cin;
using std::endl;
using std ::string;
int main ()
{
string numberMonth[12]= {"Janurary","Feburary","March","April","...
string flowerOfmonth [12]={" "};
flowerOfmonth[0]="Carnation";
flowerOfmonth[1]="Violet";
flowerOfmonth[2]="Daffodile";
flowerOfmonth[3]="Daisy";
flowerOfmonth[4]="Lily-of-the-Valley";
flowerOfmonth[5]="Rose";
flowerOfmonth[6]="Water Lily";
flowerOfmonth[7]="Gladiolus";
flowerOfmonth[8]="Aster";
flowerOfmonth[9]="Cosmos";
flowerOfmonth[10]="Chrysanthemum";
flowerOfmonth[11]="Narcissus";
//get number of month
cout %26lt;%26lt; "Enter month (1-12) :" %26lt;%26lt; endl;
cin %26gt;%26gt;numberMonth;
if (month %26gt;= 1 %26amp;%26amp; month %26lt;= 12)
cout %26lt;%26lt; flowerOfmonth[numberMonth - 1] %26lt;%26lt; endl;
//else
cout %26lt;%26lt; "Invalid month" %26lt;%26lt; endl;
//end if
//end display salary
return 0;
} //end of main function
why cant i get this to work..thanks
C++ array?
You are expecting users to enter NUMERIC representation of the months. The wording of the months is NEVER used. So, numberMonths array definition is not needed.
Define numberMonths as an INTEGER.
Look what exactly you are doing with the numberMonth variable! It's an index for an array. Yet, you defined it as string.
Reply:try to chaneg this line
string flowerOfmonth [12]={" "};
to
string flowerOfmonth [12];
OR
initialize the second array as you have done with the first one
string flowerOfmonth = {"Carnation", "Violet", "Daffodile", ... }
#include %26lt;iostream%26gt;
using std::cout;
using std::cin;
using std::endl;
using std ::string;
int main ()
{
string numberMonth[12]= {"Janurary","Feburary","March","April","...
string flowerOfmonth [12]={" "};
flowerOfmonth[0]="Carnation";
flowerOfmonth[1]="Violet";
flowerOfmonth[2]="Daffodile";
flowerOfmonth[3]="Daisy";
flowerOfmonth[4]="Lily-of-the-Valley";
flowerOfmonth[5]="Rose";
flowerOfmonth[6]="Water Lily";
flowerOfmonth[7]="Gladiolus";
flowerOfmonth[8]="Aster";
flowerOfmonth[9]="Cosmos";
flowerOfmonth[10]="Chrysanthemum";
flowerOfmonth[11]="Narcissus";
//get number of month
cout %26lt;%26lt; "Enter month (1-12) :" %26lt;%26lt; endl;
cin %26gt;%26gt;numberMonth;
if (month %26gt;= 1 %26amp;%26amp; month %26lt;= 12)
cout %26lt;%26lt; flowerOfmonth[numberMonth - 1] %26lt;%26lt; endl;
//else
cout %26lt;%26lt; "Invalid month" %26lt;%26lt; endl;
//end if
//end display salary
return 0;
} //end of main function
why cant i get this to work..thanks
C++ array?
You are expecting users to enter NUMERIC representation of the months. The wording of the months is NEVER used. So, numberMonths array definition is not needed.
Define numberMonths as an INTEGER.
Look what exactly you are doing with the numberMonth variable! It's an index for an array. Yet, you defined it as string.
Reply:try to chaneg this line
string flowerOfmonth [12]={" "};
to
string flowerOfmonth [12];
OR
initialize the second array as you have done with the first one
string flowerOfmonth = {"Carnation", "Violet", "Daffodile", ... }
Why is this C++ code wrong?
It only caculates 90 regardless of the number of inputs
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::ios;
using std::setiosflags;
int main( )
{
//declare variables
int totalCharge = 0; //counter
int charge = 0; //accumulator
int reg = 0;
int totalReg = 0;
double avg = 0.0;
cout%26lt;%26lt; "Enter number of registrants: ";
cin%26gt;%26gt; reg;
while (reg %26gt;= 0)
{
if(reg %26lt;= 3)
charge = 150 * reg;
else if (reg = 4 || reg %26lt;= 9)
charge = 100 * reg;
else (reg %26gt;= 10);
charge = 90 * reg;
totalReg += reg;
totalCharge += charge;
avg = totalCharge /totalReg;
cout %26lt;%26lt; "Total Charge: $" %26lt;%26lt; totalCharge %26lt;%26lt; endl;
cout %26lt;%26lt; "Total Registrants: " %26lt;%26lt; totalReg%26lt;%26lt; endl;
cout %26lt;%26lt; "Total Average cost: $" %26lt;%26lt; avg %26lt;%26lt; endl;
cout%26lt;%26lt; "Enter number of registrants: ";
cin%26gt;%26gt; reg;
}
return 0;}
Why is this C++ code wrong?
Hi,
I think the problem is with this part of the code:
else (reg %26gt;= 10);
charge = 90 * reg;
It should be written like this:
else if (reg %26gt;= 10)
charge = 90 * reg;
You might ask why?
because the semecolon you put after the condition
(reg %26gt;= 10);
terminates the nested if else. then the statments that comes after that, which is:
charge = 90 * reg;
is executed regardless the number of registrants.
hope my answer is helpful, good luck.
Reply:your answer is incomplete I would like to point another error in the same program.
else if(reg=4||reg%26lt;=9) here (reg=4) is wrong becuase it is an assignment operator and not relational operator Report It
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::ios;
using std::setiosflags;
int main( )
{
//declare variables
int totalCharge = 0; //counter
int charge = 0; //accumulator
int reg = 0;
int totalReg = 0;
double avg = 0.0;
cout%26lt;%26lt; "Enter number of registrants: ";
cin%26gt;%26gt; reg;
while (reg %26gt;= 0)
{
if(reg %26lt;= 3)
charge = 150 * reg;
else if (reg = 4 || reg %26lt;= 9)
charge = 100 * reg;
else (reg %26gt;= 10);
charge = 90 * reg;
totalReg += reg;
totalCharge += charge;
avg = totalCharge /totalReg;
cout %26lt;%26lt; "Total Charge: $" %26lt;%26lt; totalCharge %26lt;%26lt; endl;
cout %26lt;%26lt; "Total Registrants: " %26lt;%26lt; totalReg%26lt;%26lt; endl;
cout %26lt;%26lt; "Total Average cost: $" %26lt;%26lt; avg %26lt;%26lt; endl;
cout%26lt;%26lt; "Enter number of registrants: ";
cin%26gt;%26gt; reg;
}
return 0;}
Why is this C++ code wrong?
Hi,
I think the problem is with this part of the code:
else (reg %26gt;= 10);
charge = 90 * reg;
It should be written like this:
else if (reg %26gt;= 10)
charge = 90 * reg;
You might ask why?
because the semecolon you put after the condition
(reg %26gt;= 10);
terminates the nested if else. then the statments that comes after that, which is:
charge = 90 * reg;
is executed regardless the number of registrants.
hope my answer is helpful, good luck.
Reply:your answer is incomplete I would like to point another error in the same program.
else if(reg=4||reg%26lt;=9) here (reg=4) is wrong becuase it is an assignment operator and not relational operator Report It
Beginner C++ small question?
Hey!
I wrote this code for the question: Write a function called findReading. It should be a Reading structure reference variable as its parameter. The function should ask the user to enter values for each member of the structure. My code:
struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
void findReading(struct Reading)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; Reading.windSpeed;
cout %26lt;%26lt; "Humidity: ";
cin %26gt;%26gt; Reading.humidity;
cout %26lt;%26lt; "Fahrenheit temperature: ";
cin %26gt;%26gt; TempScale.temperature.fahrenheit;
cout %26lt;%26lt; "Centigrade temperature: ";
cin %26gt;%26gt; TempScale.temperature.centigrade;
}
}
Can u help me fix it or change it? Thanks
Beginner C++ small question?
ur parameter should be
void findReading(Reading input_readings)
input_readings is just a variable name, which is how you are going to refer to the Reading object in your function. Structs are types. So u can use them as types, just like
void some_function(int x), you need void findReadings(Reading name_of_variable);
so if you use the name input_readings, your input it going to look like this
the %26amp; passes it the memory address of the variable so you can directly work on it
void findReading( Reading%26amp; input_reading)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; input_reading.windSpeed;
ect. change all of them
also, TempScale is an object inside of readings, so it would look like
cin %26gt;%26gt; input_reading.temperature.Fahrenheit
cout %26lt;%26lt; "Centigrade temperature: ";
cin %26gt;%26gt; input_reading.temperature.centigra...
}
and u have an extra } at the end
hope that helps ya.
Reply:Your problem is the declaration of the function findReading. You write:
void findReading(struct Reading)
{
What you need is:
void findReading(struct Reading %26amp; value)
{
...
cin %26gt;%26gt; value.windSpeed;
...
cin %26gt;%26gt; value.humidity;
// etc
Now, there is a big difference when you use the '%26amp;' in the function signature.
void findReading(struct Reading value)
then you call:
fincReading(reading);
In this case, the function receives a copy of the caller's parameter. Any changes are local to the function are not reflected to the caller.
void findReading(struct Reading %26amp; value)
then you call:
fincReading(reading);
Here, the function instead of receiving a copy, gets a reference. A reference is an object that says "I'm really over there." (it's a hidden pointer). Any changes you make to the reference are made to the original. When you call the function, the parameter you pass can be modified by the function.
Reply:struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
struct Reading is a structure, you need to declare a variable of type struct Reading
In C++ you can define a function with just the type to pass in but this parameter is ignored.
Avoids the unused parameter warning.
void f(int) {}
Call this function as follows: f(0);
The function 'f' doesn't use the value passed in.
To use the value, just change the declaration to
void f(int f)
{if (f) return;} %26lt;%26lt;- Here i use the value.
void findReading(struct Reading %26amp;r)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; r.windSpeed;
cout %26lt;%26lt; "Humidity: ";
cin %26gt;%26gt; r.humidity;
tempScale t;
cout %26lt;%26lt; "Fahrenheit temperature: ";
cin %26gt;%26gt; t.fahrenheit;
// You can calculate degree Celsius
t.centigrade = (t.fahrenheit - 32.0) * 5.0 / 9.0;
r.temperature = t;
}
I wrote this code for the question: Write a function called findReading. It should be a Reading structure reference variable as its parameter. The function should ask the user to enter values for each member of the structure. My code:
struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
void findReading(struct Reading)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; Reading.windSpeed;
cout %26lt;%26lt; "Humidity: ";
cin %26gt;%26gt; Reading.humidity;
cout %26lt;%26lt; "Fahrenheit temperature: ";
cin %26gt;%26gt; TempScale.temperature.fahrenheit;
cout %26lt;%26lt; "Centigrade temperature: ";
cin %26gt;%26gt; TempScale.temperature.centigrade;
}
}
Can u help me fix it or change it? Thanks
Beginner C++ small question?
ur parameter should be
void findReading(Reading input_readings)
input_readings is just a variable name, which is how you are going to refer to the Reading object in your function. Structs are types. So u can use them as types, just like
void some_function(int x), you need void findReadings(Reading name_of_variable);
so if you use the name input_readings, your input it going to look like this
the %26amp; passes it the memory address of the variable so you can directly work on it
void findReading( Reading%26amp; input_reading)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; input_reading.windSpeed;
ect. change all of them
also, TempScale is an object inside of readings, so it would look like
cin %26gt;%26gt; input_reading.temperature.Fahrenheit
cout %26lt;%26lt; "Centigrade temperature: ";
cin %26gt;%26gt; input_reading.temperature.centigra...
}
and u have an extra } at the end
hope that helps ya.
Reply:Your problem is the declaration of the function findReading. You write:
void findReading(struct Reading)
{
What you need is:
void findReading(struct Reading %26amp; value)
{
...
cin %26gt;%26gt; value.windSpeed;
...
cin %26gt;%26gt; value.humidity;
// etc
Now, there is a big difference when you use the '%26amp;' in the function signature.
void findReading(struct Reading value)
then you call:
fincReading(reading);
In this case, the function receives a copy of the caller's parameter. Any changes are local to the function are not reflected to the caller.
void findReading(struct Reading %26amp; value)
then you call:
fincReading(reading);
Here, the function instead of receiving a copy, gets a reference. A reference is an object that says "I'm really over there." (it's a hidden pointer). Any changes you make to the reference are made to the original. When you call the function, the parameter you pass can be modified by the function.
Reply:struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
struct Reading is a structure, you need to declare a variable of type struct Reading
In C++ you can define a function with just the type to pass in but this parameter is ignored.
Avoids the unused parameter warning.
void f(int) {}
Call this function as follows: f(0);
The function 'f' doesn't use the value passed in.
To use the value, just change the declaration to
void f(int f)
{if (f) return;} %26lt;%26lt;- Here i use the value.
void findReading(struct Reading %26amp;r)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; r.windSpeed;
cout %26lt;%26lt; "Humidity: ";
cin %26gt;%26gt; r.humidity;
tempScale t;
cout %26lt;%26lt; "Fahrenheit temperature: ";
cin %26gt;%26gt; t.fahrenheit;
// You can calculate degree Celsius
t.centigrade = (t.fahrenheit - 32.0) * 5.0 / 9.0;
r.temperature = t;
}
Newb C++ Problem i cannot solve, plz HELP beginner?
#include%26lt;iostream%26gt;
using namespace std;
int main ()
{
int x;
int y;
cout %26lt;%26lt; "Enter number: " ;
cin %26gt;%26gt; x ;
cout %26lt;%26lt; " Your Number is " %26lt;%26lt; x %26lt;%26lt; endl;
cout %26lt;%26lt; "Good, Now choose another: ";
cin %26gt;%26gt; y;
cout %26lt;%26lt; " the equation will be " %26lt;%26lt; y %26lt;%26lt; cout %26lt;%26lt; " plus " %26lt;%26lt; x %26lt;%26lt;endl;
system ("pause");
return 0 ;
}
it prints :
Enter number: 5
Your Number is 5
Good, Now choose another 9
the equation will be 90x4433c4 plus 5
Press any key to continue . . .
Now i want to know why does it show "90x4433c4" and not "9"
Newb C++ Problem i cannot solve, plz HELP beginner?
Though you did not mention what do u mean by equation; whether you want to add both numbers...
The problem I see is: remove the cout which is appearing after y. In shot remove "%26lt;%26lt; cout" which is appearing after y. Your statement should appear like this:
cout %26lt;%26lt; " the equation will be " %26lt;%26lt; y %26lt;%26lt; " plus " %26lt;%26lt; x %26lt;%26lt;endl;
And if u want to add x and y then you need to do like this
cout %26lt;%26lt; " the equation will be " %26lt;%26lt; y %26lt;%26lt; " plus " %26lt;%26lt; x %26lt;%26lt; "=" %26lt;%26lt; x + y;
Reply:The 9 prints correctly
the 0x4433c4 that follows it is the hexadecimal for of the cout statement, as you only need "cout" once, it is actualy outputting the cout, if that makes sense
Reply:Remove the %26lt;%26lt; cout in the middle of the line.
You are printing out the address to the cout method.
Reply:You get some expert help from http://expert.ccietutorial.com/
Reply:Hi
The Number which you see in the consol is the address of the cout object (because 'cout' is an object from the ostream class)
and you have to remove the 'cout' to get the right number
Another thing i see in our code
you have to initialized your attributes
int x=0;
Not to make problems in other times
sweet pea
using namespace std;
int main ()
{
int x;
int y;
cout %26lt;%26lt; "Enter number: " ;
cin %26gt;%26gt; x ;
cout %26lt;%26lt; " Your Number is " %26lt;%26lt; x %26lt;%26lt; endl;
cout %26lt;%26lt; "Good, Now choose another: ";
cin %26gt;%26gt; y;
cout %26lt;%26lt; " the equation will be " %26lt;%26lt; y %26lt;%26lt; cout %26lt;%26lt; " plus " %26lt;%26lt; x %26lt;%26lt;endl;
system ("pause");
return 0 ;
}
it prints :
Enter number: 5
Your Number is 5
Good, Now choose another 9
the equation will be 90x4433c4 plus 5
Press any key to continue . . .
Now i want to know why does it show "90x4433c4" and not "9"
Newb C++ Problem i cannot solve, plz HELP beginner?
Though you did not mention what do u mean by equation; whether you want to add both numbers...
The problem I see is: remove the cout which is appearing after y. In shot remove "%26lt;%26lt; cout" which is appearing after y. Your statement should appear like this:
cout %26lt;%26lt; " the equation will be " %26lt;%26lt; y %26lt;%26lt; " plus " %26lt;%26lt; x %26lt;%26lt;endl;
And if u want to add x and y then you need to do like this
cout %26lt;%26lt; " the equation will be " %26lt;%26lt; y %26lt;%26lt; " plus " %26lt;%26lt; x %26lt;%26lt; "=" %26lt;%26lt; x + y;
Reply:The 9 prints correctly
the 0x4433c4 that follows it is the hexadecimal for of the cout statement, as you only need "cout" once, it is actualy outputting the cout, if that makes sense
Reply:Remove the %26lt;%26lt; cout in the middle of the line.
You are printing out the address to the cout method.
Reply:You get some expert help from http://expert.ccietutorial.com/
Reply:Hi
The Number which you see in the consol is the address of the cout object (because 'cout' is an object from the ostream class)
and you have to remove the 'cout' to get the right number
Another thing i see in our code
you have to initialized your attributes
int x=0;
Not to make problems in other times
sweet pea
Help c++ programing problem?
//load and save... i hope!!!
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
using namespace std;
int main()
{
system ("color 2B");
int choice;
int number;
cout %26lt;%26lt; "hi.\n\n\nContinue:1\nNew:2\n\n\n";
cin %26gt;%26gt; choice;
if (choice==2)
{ cout %26lt;%26lt; "\n\nEnter number.";
cin %26gt;%26gt; number;
cout %26lt;%26lt; "Number stored";
}
if (choice==1)
{
cout %26lt;%26lt; "\n\n\nYour number is: ";
cout %26lt;%26lt; number;
}
system ("PAUSE");
return 0;
}
How do I make it to where when the user restarts the program it will display the number they entered before? everytime i try to load a number i entered it always says 49. plzz post
Ive made this post several times with no luck so someone plz post. Also, leave specific details on what I need to add or change. Even if you have to show me the source code that im suppose to have lol.
Help c++ programing problem?
You need to write the number to a file. You have the correct header to do this.. I just don't think you fully understand how to use it. for a full tutorial, go to http://www.cprogramming.com/tutorial/les...
Here's a program for you
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
using namespace std;
int main(){
system ("color 2B");
int choice;
char number[4];
ofstream thing("number.txt", ios::app);//will make file number.txt
ifstream read;
start:
cout %26lt;%26lt; "Continue:1\nNew:2\n";
cin %26gt;%26gt; choice;
if (choice == 1){
read.open("number.txt");
if (!read){
cout%26lt;%26lt;"We've encountered a problem.. I'm sorry\n";
system("pause");
}
while(read %26gt;%26gt; number){
cout%26lt;%26lt;"Your number is: "%26lt;%26lt; number%26lt;%26lt;endl;
cout%26lt;%26lt;"\n";
read.close();
}
}
else if(choice == 2){
cout %26lt;%26lt; "\n\nEnter number.";
cin %26gt;%26gt; number;
thing %26lt;%26lt; number;
cout%26lt;%26lt;"Number stored";
thing.close();
}
else{
cout%26lt;%26lt;"Invalid choice";
goto start;
}
system("pause");
}
I use dev-c++, just in case this doesn't compile right for you.
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
using namespace std;
int main()
{
system ("color 2B");
int choice;
int number;
cout %26lt;%26lt; "hi.\n\n\nContinue:1\nNew:2\n\n\n";
cin %26gt;%26gt; choice;
if (choice==2)
{ cout %26lt;%26lt; "\n\nEnter number.";
cin %26gt;%26gt; number;
cout %26lt;%26lt; "Number stored";
}
if (choice==1)
{
cout %26lt;%26lt; "\n\n\nYour number is: ";
cout %26lt;%26lt; number;
}
system ("PAUSE");
return 0;
}
How do I make it to where when the user restarts the program it will display the number they entered before? everytime i try to load a number i entered it always says 49. plzz post
Ive made this post several times with no luck so someone plz post. Also, leave specific details on what I need to add or change. Even if you have to show me the source code that im suppose to have lol.
Help c++ programing problem?
You need to write the number to a file. You have the correct header to do this.. I just don't think you fully understand how to use it. for a full tutorial, go to http://www.cprogramming.com/tutorial/les...
Here's a program for you
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
using namespace std;
int main(){
system ("color 2B");
int choice;
char number[4];
ofstream thing("number.txt", ios::app);//will make file number.txt
ifstream read;
start:
cout %26lt;%26lt; "Continue:1\nNew:2\n";
cin %26gt;%26gt; choice;
if (choice == 1){
read.open("number.txt");
if (!read){
cout%26lt;%26lt;"We've encountered a problem.. I'm sorry\n";
system("pause");
}
while(read %26gt;%26gt; number){
cout%26lt;%26lt;"Your number is: "%26lt;%26lt; number%26lt;%26lt;endl;
cout%26lt;%26lt;"\n";
read.close();
}
}
else if(choice == 2){
cout %26lt;%26lt; "\n\nEnter number.";
cin %26gt;%26gt; number;
thing %26lt;%26lt; number;
cout%26lt;%26lt;"Number stored";
thing.close();
}
else{
cout%26lt;%26lt;"Invalid choice";
goto start;
}
system("pause");
}
I use dev-c++, just in case this doesn't compile right for you.
Subscribe to:
Posts (Atom)