Help! My average variable is not converting from an integrer into a double. I think there is smth wrong with the static cast.
#include %26lt;iostream%26gt;
#include %26lt;cmath%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
int main()
{
int num;
int count=0;
int total=0;
do{
cout %26lt;%26lt; "Enter positive number press -1 to exit\n";
cin %26gt;%26gt; num;
if (num %26gt; 0){
total+=num;
count++;}
}while (num != -1);
double average = double(total/count); // is there smth wrong?
cout %26lt;%26lt; "Total is " %26lt;%26lt; total %26lt;%26lt; endl;
cout %26lt;%26lt; "Count is " %26lt;%26lt; count %26lt;%26lt; endl;
cout %26lt;%26lt; "Average of non negative numbers is " %26lt;%26lt; fixed %26lt;%26lt; showpoint %26lt;%26lt; setprecision(2) %26lt;%26lt; average %26lt;%26lt;endl;
return 0;
}
C++ programming help?
Integer operated with integer is integer. change it to double(total)/count
Saturday, May 22, 2010
How would rewrite this C++ problem using VOID FUNCTIONS?
#include %26lt;iostream%26gt;
using namespace std;
//funtions prototypes
double getGross(int, double);
double getNet(double);
int main()
{
//declare variables
string name = "";
double rate = 0.0;
int hours = 0;
double grossPay = 0.0;
double netPay = 0.0;
//get input
cout %26lt;%26lt; "Enter employee's name: " %26lt;%26lt; endl;
getline (cin, name);
cout %26lt;%26lt; "Enter hours worked: " %26lt;%26lt; endl;
cin %26gt;%26gt; hours;
cout %26lt;%26lt; "Enter pay rate: " %26lt;%26lt; endl;
cin %26gt;%26gt; rate;
//function calls
grossPay = getGross(hours, rate);
netPay = getNet(grossPay);
//display information
cout %26lt;%26lt; "Employee Name: " %26lt;%26lt; name %26lt;%26lt; endl;
cout %26lt;%26lt; "Gross pay: " %26lt;%26lt; grossPay %26lt;%26lt; endl;
cout %26lt;%26lt; "Net pay: " %26lt;%26lt; netPay %26lt;%26lt; endl;
return 0;
}
//***PROGRAM DEFINED FUCTION***
double getGross (int hours, double rate)
{
double answer = 0.0;
answer = hours * rate;
return answer;
}
How would rewrite this C++ problem using VOID FUNCTIONS?
Alternatively, you could send a pointer to the function for it to store the value in. This would better maintain good OO principles like 'Information Hiding', 'Code Reuse' and 'Loose Coupling'.
For example if in the main routine instead of:
netPay = getNet(grossPay);
you would say
getNet(grossPay, %26amp;netPay);
and the getNet function would be:
void getNet (double grossPay, double *answer)
{
*answer = grossPay * .90;
}
Or better yet
inline void getNet(double grossPay, double *answer) {*answer = grossPay * .90;}
Reply:Above is a fine answer, and this syntax works just as well
void func( int param, int %26amp;answer);
int main(int c, char **argv)
{
int iParam = 1;
int answer;
func( param, answer);
}
void func( int param, int %26amp;answer)
{
answer = param;
}
I'm just not a fan of * because to me * means memory that I have to manage :)
Reply:In a void function, you would print the information during the function. That means that you'll need to move your cout statements for your gross pay and your net pay to their respective functions. That way you aren't returning anything because everything is done inside that function (the calculation AND the cout).
void getNet(double grossPay)
{
double answer = 0.0;
answer = grossPay * .90;
cout %26lt;%26lt; "Gross pay: " %26lt;%26lt; grossPay %26lt;%26lt; endl;
}
using namespace std;
//funtions prototypes
double getGross(int, double);
double getNet(double);
int main()
{
//declare variables
string name = "";
double rate = 0.0;
int hours = 0;
double grossPay = 0.0;
double netPay = 0.0;
//get input
cout %26lt;%26lt; "Enter employee's name: " %26lt;%26lt; endl;
getline (cin, name);
cout %26lt;%26lt; "Enter hours worked: " %26lt;%26lt; endl;
cin %26gt;%26gt; hours;
cout %26lt;%26lt; "Enter pay rate: " %26lt;%26lt; endl;
cin %26gt;%26gt; rate;
//function calls
grossPay = getGross(hours, rate);
netPay = getNet(grossPay);
//display information
cout %26lt;%26lt; "Employee Name: " %26lt;%26lt; name %26lt;%26lt; endl;
cout %26lt;%26lt; "Gross pay: " %26lt;%26lt; grossPay %26lt;%26lt; endl;
cout %26lt;%26lt; "Net pay: " %26lt;%26lt; netPay %26lt;%26lt; endl;
return 0;
}
//***PROGRAM DEFINED FUCTION***
double getGross (int hours, double rate)
{
double answer = 0.0;
answer = hours * rate;
return answer;
}
How would rewrite this C++ problem using VOID FUNCTIONS?
Alternatively, you could send a pointer to the function for it to store the value in. This would better maintain good OO principles like 'Information Hiding', 'Code Reuse' and 'Loose Coupling'.
For example if in the main routine instead of:
netPay = getNet(grossPay);
you would say
getNet(grossPay, %26amp;netPay);
and the getNet function would be:
void getNet (double grossPay, double *answer)
{
*answer = grossPay * .90;
}
Or better yet
inline void getNet(double grossPay, double *answer) {*answer = grossPay * .90;}
Reply:Above is a fine answer, and this syntax works just as well
void func( int param, int %26amp;answer);
int main(int c, char **argv)
{
int iParam = 1;
int answer;
func( param, answer);
}
void func( int param, int %26amp;answer)
{
answer = param;
}
I'm just not a fan of * because to me * means memory that I have to manage :)
Reply:In a void function, you would print the information during the function. That means that you'll need to move your cout statements for your gross pay and your net pay to their respective functions. That way you aren't returning anything because everything is done inside that function (the calculation AND the cout).
void getNet(double grossPay)
{
double answer = 0.0;
answer = grossPay * .90;
cout %26lt;%26lt; "Gross pay: " %26lt;%26lt; grossPay %26lt;%26lt; endl;
}
C++ hourglass loop?
I'm attempting to write a program that makes an hourglass design using asterisks (*).The user inputs a number of rows and the program makes the hourglass based on how many rows are specified.
Here is an example: This would show if 5 was entered.
*****
-***
--*
-***
*****
This is what I have so far:
int main()
{
int n, rows;
cout %26lt;%26lt; "Enter number of rows (no more than 23):" %26lt;%26lt; flush;
cin %26gt;%26gt; rows;
for (n = 1; n %26lt;= rows; n++)
{
if (n %26lt;= rows)
cout %26lt;%26lt; '*' %26lt;%26lt; flush;
}
if ((rows + 1) % 2)
cout %26lt;%26lt; '*' %26lt;%26lt; flush;
else
cout %26lt;%26lt; " " %26lt;%26lt; flush;
return 0;
This amount of code gets me the first line of *'s. I can only use cntrl structures: if, if else, switch, while, for, do-while; and can only use single characters and single spaces. I'm not looking for the exact code because I do want to learn this, but I am stuck and any pointers would be greatly appreciated
C++ hourglass loop?
#include "CHourGlass.h"
#include %26lt;string%26gt;
#include %26lt;iostream%26gt;
using namespace std;
CHourGlass::CHourGlass(int aRows)
{
if( aRows %26lt; 3 || aRows %26gt; 23 )
throw new string("row number violation");
m_height = aRows;
m_width = 1+((aRows/2)*2);
}
CHourGlass::~CHourGlass()
{
}
void CHourGlass::display()
{
int iWidth = m_width;
for( int i = 0; i%26lt; m_height;i++)
{
cout %26lt;%26lt; center(abs(iWidth))%26lt;%26lt; endl;
iWidth-=2;
if( iWidth == -1)
iWidth -=2;
}
}
string CHourGlass::center( int iCount)
{
int iSpace = (m_width-iCount)/2;
string result = string(iSpace,' ')+string(iCount, '*');
return result;
}
Reply:The first thing you need to do is identify the algorithm/relationship that you want your program to reflect. For example, given the number of rows the user wants, how do you calculate the number of stars each row will have? From your example of 5 rows it appears that you do:
5
5-2
5-2-2
5-2
5
Which would reflect an hourglass, however what if you only have 4 or 3 or 2 rows?
Identify this first and your program will easily fall into place.
Good luck!
Here is an example: This would show if 5 was entered.
*****
-***
--*
-***
*****
This is what I have so far:
int main()
{
int n, rows;
cout %26lt;%26lt; "Enter number of rows (no more than 23):" %26lt;%26lt; flush;
cin %26gt;%26gt; rows;
for (n = 1; n %26lt;= rows; n++)
{
if (n %26lt;= rows)
cout %26lt;%26lt; '*' %26lt;%26lt; flush;
}
if ((rows + 1) % 2)
cout %26lt;%26lt; '*' %26lt;%26lt; flush;
else
cout %26lt;%26lt; " " %26lt;%26lt; flush;
return 0;
This amount of code gets me the first line of *'s. I can only use cntrl structures: if, if else, switch, while, for, do-while; and can only use single characters and single spaces. I'm not looking for the exact code because I do want to learn this, but I am stuck and any pointers would be greatly appreciated
C++ hourglass loop?
#include "CHourGlass.h"
#include %26lt;string%26gt;
#include %26lt;iostream%26gt;
using namespace std;
CHourGlass::CHourGlass(int aRows)
{
if( aRows %26lt; 3 || aRows %26gt; 23 )
throw new string("row number violation");
m_height = aRows;
m_width = 1+((aRows/2)*2);
}
CHourGlass::~CHourGlass()
{
}
void CHourGlass::display()
{
int iWidth = m_width;
for( int i = 0; i%26lt; m_height;i++)
{
cout %26lt;%26lt; center(abs(iWidth))%26lt;%26lt; endl;
iWidth-=2;
if( iWidth == -1)
iWidth -=2;
}
}
string CHourGlass::center( int iCount)
{
int iSpace = (m_width-iCount)/2;
string result = string(iSpace,' ')+string(iCount, '*');
return result;
}
Reply:The first thing you need to do is identify the algorithm/relationship that you want your program to reflect. For example, given the number of rows the user wants, how do you calculate the number of stars each row will have? From your example of 5 rows it appears that you do:
5
5-2
5-2-2
5-2
5
Which would reflect an hourglass, however what if you only have 4 or 3 or 2 rows?
Identify this first and your program will easily fall into place.
Good luck!
C++ array question?
I madethe changes you guys recommended, but I am getting errors. Any ideas? Thanks!!
//Reading ages from user and printing out values in reverse order
//************************************...
#include%26lt;iostream%26gt;
using namespace std;
int main()
{
int ageValue;
int count;
char choice;
int loopCounter=0;
cout%26lt;%26lt;"Enter the age of the youngest family member"%26lt;%26lt;endl;
cin%26gt;%26gt;ageValue;
cout%26lt;%26lt;"Are there any more family members(Y for yes N for no)";
cin%26gt;%26gt;choice;
if (choice=='Y'||choice=='y')
do
{
count=count+1;
cout%26lt;%26lt;"Enter the age of the next youngest family member"%26lt;%26lt;endl;
cin%26gt;%26gt;ageValue[count];
}
while(choice!='N'||choice!='n');
for(loopCounter=count;loopCounter=0; loopCounter--)
{
cout%26lt;%26lt;ageValue[count];
count--;
}
return 0;
}
C++ array question?
From what I can see, and others have said;
(1) the count is not initialized, the way you want the program to run it should be initialized as -1. When the count = count + 1 is called, it will go to 0, the first index in the array.
(2) ageValue should also be initilized in case of an incorrect entry from user, then the value will remain. Probably be best for -1, as no-one is this age, if you want to use the variable as the input, or you need to init it to an array, and change the first cin %26gt;%26gt; ageValue to cin %26gt;%26gt; ageValue[count] then you can do (1) and init count as 0.
(3) The problem with using an array, you will limit the program to 150, or 255 people. Probably be best using a vector from the standard library. Hope you know about this.
(4) As you are only using loopCounter for the loop at the bottom, you can declare it in the for loop. for (int loopCounter = count; blah, or call it 'i' instead, like for (int i = count; i %26gt;= 0; i++);
(5) The statement count = count + 1 can be shortened two ways
(5.1) count += 1 (use if adding more than 1)
(5.2) count++ (preferred for adding 1)
(6) The loop in (4) has to be changed from loopCounter = 0 to loopCounter %26gt;= 0; The reason is, the statement needs to return true for the loop to continue, and if the count is greater than 1 it will not be equal to 0, and not continue. Therefore, if only one person was input, they will be printed, but any other amount will not. The changed statement will return true until the counter drops below 0 and will print out the desired results.
I think thats about it, I did not test these changes or anything.
[edit]
(7) You could even change the statement while (choice != 'N, blah to while (choice == 'Y, blah so the if statement above is the same, or you could change the if statement to if (choice != 'N', blah.
(8) You have not asked the user if there are any more family members in the do while loop and the while condition will always be true. Just copy what you have from the top, it is bloating the code, but just while you learn, and paste it in the do while loop.
Reply:You have made an error in array .you haven't declared array.
Declare it as
int ageValue[]=new 1nt[50];
initialize count=0 in the beginning itself
then change the first cin statement as
cin%26gt;%26gt;ageValue[1];
then, change the for loop as
for(loopCounter=count;loopCounter%26gt;=1;l...
delete count-- in the loop
Reply:while(choice!='N'||choice!='n'...
yout '=' should be '==' in both places in this line. You also didn't initialize 'count'.
What exact error message do you get? Compile or runtime?
Reply:WTF does that mean?!?!?!?!?
Reply:you should say int ageValue[255];
right now ageValue is not an array but ONE variable.
thats all the errors I can spot and it is a big one so try that and if it doesnt work email the source code to me.
right now, yahoo is putting in the annoying ... things.
Reply:hmmm
Reply:It's been a while since I wrote C++ programs, but it might help if you told us what errors you are getting.
I think I would do it more or less like this, providing my memory of c++ coding is still acurate, at any rate the method should work:
int ageValue[];
int count;
char choice;
cout %26lt;%26lt; "Enter the age of the youngest family member" %26lt;%26lt; endl;
cin %26gt;%26gt; ageValue[0];
cout %26lt;%26lt; "Are there any more family members";
cin %26gt;%26gt; choice;
for(count = 1; choice == 'Y' || choice == 'y'; count++)
{
cout %26lt;%26lt; "Enter the age of the next youngest" %26lt;%26lt; endl;
cin %26gt;%26gt; ageValue[count];
cout %26lt;%26lt; "Are there more family members?";
cin %26gt;%26gt; choice;
}
return 0;
lady palm
//Reading ages from user and printing out values in reverse order
//************************************...
#include%26lt;iostream%26gt;
using namespace std;
int main()
{
int ageValue;
int count;
char choice;
int loopCounter=0;
cout%26lt;%26lt;"Enter the age of the youngest family member"%26lt;%26lt;endl;
cin%26gt;%26gt;ageValue;
cout%26lt;%26lt;"Are there any more family members(Y for yes N for no)";
cin%26gt;%26gt;choice;
if (choice=='Y'||choice=='y')
do
{
count=count+1;
cout%26lt;%26lt;"Enter the age of the next youngest family member"%26lt;%26lt;endl;
cin%26gt;%26gt;ageValue[count];
}
while(choice!='N'||choice!='n');
for(loopCounter=count;loopCounter=0; loopCounter--)
{
cout%26lt;%26lt;ageValue[count];
count--;
}
return 0;
}
C++ array question?
From what I can see, and others have said;
(1) the count is not initialized, the way you want the program to run it should be initialized as -1. When the count = count + 1 is called, it will go to 0, the first index in the array.
(2) ageValue should also be initilized in case of an incorrect entry from user, then the value will remain. Probably be best for -1, as no-one is this age, if you want to use the variable as the input, or you need to init it to an array, and change the first cin %26gt;%26gt; ageValue to cin %26gt;%26gt; ageValue[count] then you can do (1) and init count as 0.
(3) The problem with using an array, you will limit the program to 150, or 255 people. Probably be best using a vector from the standard library. Hope you know about this.
(4) As you are only using loopCounter for the loop at the bottom, you can declare it in the for loop. for (int loopCounter = count; blah, or call it 'i' instead, like for (int i = count; i %26gt;= 0; i++);
(5) The statement count = count + 1 can be shortened two ways
(5.1) count += 1 (use if adding more than 1)
(5.2) count++ (preferred for adding 1)
(6) The loop in (4) has to be changed from loopCounter = 0 to loopCounter %26gt;= 0; The reason is, the statement needs to return true for the loop to continue, and if the count is greater than 1 it will not be equal to 0, and not continue. Therefore, if only one person was input, they will be printed, but any other amount will not. The changed statement will return true until the counter drops below 0 and will print out the desired results.
I think thats about it, I did not test these changes or anything.
[edit]
(7) You could even change the statement while (choice != 'N, blah to while (choice == 'Y, blah so the if statement above is the same, or you could change the if statement to if (choice != 'N', blah.
(8) You have not asked the user if there are any more family members in the do while loop and the while condition will always be true. Just copy what you have from the top, it is bloating the code, but just while you learn, and paste it in the do while loop.
Reply:You have made an error in array .you haven't declared array.
Declare it as
int ageValue[]=new 1nt[50];
initialize count=0 in the beginning itself
then change the first cin statement as
cin%26gt;%26gt;ageValue[1];
then, change the for loop as
for(loopCounter=count;loopCounter%26gt;=1;l...
delete count-- in the loop
Reply:while(choice!='N'||choice!='n'...
yout '=' should be '==' in both places in this line. You also didn't initialize 'count'.
What exact error message do you get? Compile or runtime?
Reply:WTF does that mean?!?!?!?!?
Reply:you should say int ageValue[255];
right now ageValue is not an array but ONE variable.
thats all the errors I can spot and it is a big one so try that and if it doesnt work email the source code to me.
right now, yahoo is putting in the annoying ... things.
Reply:hmmm
Reply:It's been a while since I wrote C++ programs, but it might help if you told us what errors you are getting.
I think I would do it more or less like this, providing my memory of c++ coding is still acurate, at any rate the method should work:
int ageValue[];
int count;
char choice;
cout %26lt;%26lt; "Enter the age of the youngest family member" %26lt;%26lt; endl;
cin %26gt;%26gt; ageValue[0];
cout %26lt;%26lt; "Are there any more family members";
cin %26gt;%26gt; choice;
for(count = 1; choice == 'Y' || choice == 'y'; count++)
{
cout %26lt;%26lt; "Enter the age of the next youngest" %26lt;%26lt; endl;
cin %26gt;%26gt; ageValue[count];
cout %26lt;%26lt; "Are there more family members?";
cin %26gt;%26gt; choice;
}
return 0;
lady palm
I need some help with a C++ program?
Hey I keep getting errors in this program and im not sure how to fix them, the program is supposed to tally up votes and print out winners in 6 different races. I'll post the program first and then my question:
#include%26lt;iostream%26gt;
#include%26lt;fstream%26gt;
using namespace std;
void tally(int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;...
void print(int, int, int, int);
int main() {
cout%26lt;%26lt;"CS-114 Ballot Counter"%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Presidency:"%26lt;%26lt;endl;
print(1, 2, p1, p2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Vice Presidency:"%26lt;%26lt;endl;
print(3, 4, vp1, vp2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Secretary:"%26lt;%26lt;endl;
print(5, 6, s1, s2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Treasurer:"%26lt;%26lt;endl;
print(7, 8, t1, t2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Judge:"%26lt;%26lt;endl;
print(9, 10, j1, j2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Senator:"%26lt;%26lt;endl;
print(11, 12, sen1, sen2);
}
I need some help with a C++ program?
Undeclared identifier means you're using a variable that you haven't declared yet, and the compiler doesn't know what it is since you haven't told it. You need to declare p1, p2, vp1, etc before you ask the computer to print them. So at the very top of your main(), you need this statement:
int p1, p2, vp1 .... sen1, sen2;
You probably need to initialize all of them to zero too before you start; otherwise they will contain garbage values.
Next, if your program really executes in that order, I think you're trying to print the results before you actually collect them, so you need to open ballot.txt and tally up the votes before you call print(1, 2, p1, p2) etc.
Also, you're getting voter %26gt;%26gt; vote before your while loop, then doing it again once you get inside the loop, so you're always going to miss the first vote, unless the first entry in ballot.txt is a dummy value. You should delete the first voter %26gt;%26gt; vote; statement. If the file's empty, you'll get an error there anyway.
Your big long if block should be a switch-case statement instead of a series of if's. I believe the syntax is like this:
switch(vote)
{
case 1: p1++; break;
case 2: p2++; break;
default: cerr %26lt;%26lt; "Invalid vote";
}
The default case is for when none of the other cases match. You currently have p1+=0 for that, which makes no sense. You should just do nothing, or at least output an error message.
Good luck with your program!
#include%26lt;iostream%26gt;
#include%26lt;fstream%26gt;
using namespace std;
void tally(int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;...
void print(int, int, int, int);
int main() {
cout%26lt;%26lt;"CS-114 Ballot Counter"%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Presidency:"%26lt;%26lt;endl;
print(1, 2, p1, p2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Vice Presidency:"%26lt;%26lt;endl;
print(3, 4, vp1, vp2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Secretary:"%26lt;%26lt;endl;
print(5, 6, s1, s2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Treasurer:"%26lt;%26lt;endl;
print(7, 8, t1, t2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Judge:"%26lt;%26lt;endl;
print(9, 10, j1, j2);
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Senator:"%26lt;%26lt;endl;
print(11, 12, sen1, sen2);
}
I need some help with a C++ program?
Undeclared identifier means you're using a variable that you haven't declared yet, and the compiler doesn't know what it is since you haven't told it. You need to declare p1, p2, vp1, etc before you ask the computer to print them. So at the very top of your main(), you need this statement:
int p1, p2, vp1 .... sen1, sen2;
You probably need to initialize all of them to zero too before you start; otherwise they will contain garbage values.
Next, if your program really executes in that order, I think you're trying to print the results before you actually collect them, so you need to open ballot.txt and tally up the votes before you call print(1, 2, p1, p2) etc.
Also, you're getting voter %26gt;%26gt; vote before your while loop, then doing it again once you get inside the loop, so you're always going to miss the first vote, unless the first entry in ballot.txt is a dummy value. You should delete the first voter %26gt;%26gt; vote; statement. If the file's empty, you'll get an error there anyway.
Your big long if block should be a switch-case statement instead of a series of if's. I believe the syntax is like this:
switch(vote)
{
case 1: p1++; break;
case 2: p2++; break;
default: cerr %26lt;%26lt; "Invalid vote";
}
The default case is for when none of the other cases match. You currently have p1+=0 for that, which makes no sense. You should just do nothing, or at least output an error message.
Good luck with your program!
Need help with a C++ program please?
ok I wrote something not sure how right it is but i do need some help on it
here is what i have:
int menu(int x){
cout%26lt;%26lt;"Press 1 to encrypt, 2 to decrypt, 3 to quit)"%26lt;%26lt;endl;
cin%26gt;%26gt;x;
return x;
}
void encrypt(char letter){
cout%26lt;%26lt;"Enter a message to encrypt"%26lt;%26lt;endl;
cin%26gt;%26gt;letter;
while (letter!='!'){
if (letter=='k') cout%26lt;%26lt;"1 ";
then 26 more if statements for letters
cin%26gt;%26gt;letter;
}
return;
}
void decrypt(int code){
cin%26gt;%26gt;code;
while (code!=0){
if (code==1) cout%26lt;%26lt;"k";
26 more if statements
cin%26gt;%26gt;code;
}
return;
}
int main(){
int choice;
int shift = 7; // default shift
choice = menu();************
while (choice != 3){
if (choice == 1){
cout %26lt;%26lt; "Enter the 'shift' value: ";
cin %26gt;%26gt; shift;
encrypt(shift); }
else
decrypt();************
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;endl;
choice = menu(); **********
}
return 0
Need help with a C++ program please?
Your function menu() does not take zero arguments. It need one argument, as per the function definition. What you want is
int menu(),
and what you have done is
int menu(int x)
And you are not using this parameter x at all.
Reply:Surely: your menu() function requires int as an argument.
Check you code thoroughly :-)
here is what i have:
int menu(int x){
cout%26lt;%26lt;"Press 1 to encrypt, 2 to decrypt, 3 to quit)"%26lt;%26lt;endl;
cin%26gt;%26gt;x;
return x;
}
void encrypt(char letter){
cout%26lt;%26lt;"Enter a message to encrypt"%26lt;%26lt;endl;
cin%26gt;%26gt;letter;
while (letter!='!'){
if (letter=='k') cout%26lt;%26lt;"1 ";
then 26 more if statements for letters
cin%26gt;%26gt;letter;
}
return;
}
void decrypt(int code){
cin%26gt;%26gt;code;
while (code!=0){
if (code==1) cout%26lt;%26lt;"k";
26 more if statements
cin%26gt;%26gt;code;
}
return;
}
int main(){
int choice;
int shift = 7; // default shift
choice = menu();************
while (choice != 3){
if (choice == 1){
cout %26lt;%26lt; "Enter the 'shift' value: ";
cin %26gt;%26gt; shift;
encrypt(shift); }
else
decrypt();************
cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;endl;
choice = menu(); **********
}
return 0
Need help with a C++ program please?
Your function menu() does not take zero arguments. It need one argument, as per the function definition. What you want is
int menu(),
and what you have done is
int menu(int x)
And you are not using this parameter x at all.
Reply:Surely: your menu() function requires int as an argument.
Check you code thoroughly :-)
C++ program problems once again...?
#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".
#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".
Subscribe to:
Posts (Atom)