Saturday, May 22, 2010

I need help with a c++ program please?

hey Im getting some errors that dont make sense to me and i dont know how to fix


ill post my code and then the errors


#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;


int p1,p2,vp1,vp2,s1,s2,t1,t2,j1,j2,sen1,sen...








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 help with a c++ program please?
What are the errors?

bottle palm

Why wont this C++ program show any Odd numbers!!!!!?

This works and shows the factors or tells if the number is a prime number but doesn't show ODD factors please help!! I need to turn this tomorrow:


#include %26lt;iostream%26gt;


using namespace std;


int prime(int num);


int main()


{


int num;


cout%26lt;%26lt;"Enter number: ";


cin%26gt;%26gt;num;


if(prime(num)==1)


cout%26lt;%26lt;"The number is prime";


else


{


cout%26lt;%26lt;"The number is not prime, factors are\n1\n";


if( num%2==0)


for(int j=2;j%26lt;num;j+=2)


{


if(num%j==0)


cout%26lt;%26lt;j%26lt;%26lt;endl;


}


for( int n=num;n%26lt;=num;n/=2)


{


if(num%n==0)


cout%26lt;%26lt;n%26lt;%26lt;endl;


}


for(int m=3;m%26lt;num;m+=3)


{


if(num%m==0)


cout%26lt;%26lt;m%26lt;%26lt;endl;


}


}


}


int prime(int num)


{


if(num %26lt;= 1)


{


return false;


}


if(num == 2)


{


return true;


}


if ( num%2 == 0)


{


return false;


}





for (int n=3;n*n %26lt;= num;n+=2)


{ if (num%n == 0)


return false;


}


return true;


}

Why wont this C++ program show any Odd numbers!!!!!?
I rewrote your program:


#include %26lt;iostream%26gt;


using namespace std;


int prime(int num);


int main()


{


int num;


cout%26lt;%26lt;"Enter number: ";


cin%26gt;%26gt;num;


if(prime(num)==1)


cout%26lt;%26lt;"The number is prime";


else


{


cout%26lt;%26lt;"The number is not prime, factors are\n1\n";


{


for(int j=2;j%26lt;num;j+=2)


{


if(num%j==0)


cout%26lt;%26lt;j%26lt;%26lt;endl;


if((num%(j-1)==0) %26amp;%26amp;(j%26gt;2))


{


cout%26lt;%26lt;(j-1)%26lt;%26lt;endl;


}


}


}


}





cin%26gt;%26gt;num;


return 0;


}





int prime(int num)


{


if(num %26lt;= 1)


{


return false;


}


if(num == 2)


{


return true;


}


if ( num%2 == 0)


{


return false;


}





for (int n=3;n*n %26lt;= num;n+=2)


{


if (num%n == 0)


return false;


}


return true;


}








Change this piece of code :


for( int n=num;n%26lt;=num;n/=2)


{


if(num%n==0)


cout%26lt;%26lt;n%26lt;%26lt;endl;


}





to this:





for( int n=1;n%26lt;=num;n+=2)


{


if(num%n==0)


cout%26lt;%26lt;n%26lt;%26lt;endl;


}


C++ help! can please me with C++! i am just learning!?

this program should be an IF statement that displays the string "Firebird" when the user enters the letter F (in any case).





this is my code, but it's not working! can someone tell me what am i doing wrong?





#include %26lt;iostream%26gt;


#include %26lt;algorithm%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::transform;


using std::fixed;


using std::setprecision;





int main()


{


//declare variable


char letter = 'F';


string word = "";








//enter input


cout %26lt;%26lt; "Enter a letter: ";


cin %26gt;%26gt; letter;





//convert letter to uppercase


transform(letter.begin(), letter.end(), letter.begin(), toupper);





//display output


if (letter == 'F')


word = Firebird;


//end if





cout %26lt;%26lt; "Word: " %26lt;%26lt; word %26lt;%26lt; endl;


return 0;


} //end of main function

C++ help! can please me with C++! i am just learning!?
why are you making it so complicated?


it's a very simple program:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


char letter='f';


string word="";





cout %26lt;%26lt; "Enter a letter: ";


cin %26gt;%26gt; letter;





//display output


if ((letter == 'F') || (letter =='f'))


word="Firebird";


else


word="N/A";





cout %26lt;%26lt; "Word: " %26lt;%26lt; word %26lt;%26lt; endl;





return 0;


}





Replace N/A with whatever is appropriate. Some other #include-s may be necessary to successfully compile this program.





Good luck!
Reply:int main()


{


char letter;


cout %26lt;%26lt;"Enter Letter: ";


cin %26gt;%26gt; letter;


if (letter == 'F' || letter == 'f')


cout %26lt;%26lt;"\nWord: Firebird"%26lt;%26lt;endl;


return (0);


}





you only need to include iostream


and


using namespace std;
Reply:First off you don't need


"#include %26lt;algorithm%26gt;


#include %26lt;iomanip%26gt;"


You can just delete those





"using std::cout;


using std::cin;


using std::endl;


using std::transform;


using std::fixed;


using std::setprecision;"


All these things can be replaced with "Using namespace std;"





Since the progarm is asking you for "letter f" you don't need a string, you can use char f = 'f';





Now you can ask for user input





cout %26lt;%26lt; "Enter a letter: ";


cin %26gt;%26gt; letter;





You don't really need "transform(letter.begin(), letter.end(), letter.begin(), toupper);"





Now write an if statement, to decide if it is a F or f





if (letter == 'F' || letter == 'f')





"word = Firebird;"


This is not valid, you didn't create any variable for "word"


instead, you can use "cout %26lt;%26lt; "Firebird";





Now you need an else statement


If letter isnt F or f then do this... %26lt;something%26gt;





Now if you were to put everything together, it will look like this





#include %26lt;iostream%26gt;


using namespace std;





int main()


{





char letter = 'F';


// declare variables





cout %26lt;%26lt; "Enter a letter: ";


// ask for user input





cin %26gt;%26gt; letter;


// user inputs something





if (letter == 'F' || letter == 'f')


cout %26lt;%26lt; "Firebird";


else


cout %26lt;%26lt; "Word: " %26lt;%26lt; letter %26lt;%26lt; endl;


// an if statement, IF letter is F or f then display Firebird, if it is not, then display the word





return 0;


}
Reply:Firebird should be quoted thus it is a string.


About c++ (if and else)?

there is an error saying "illegal else without matching if"


how will i correct this one?





# include %26lt;iostream.h%26gt;


int main ()


{


// conditional statement


int age;


cout%26lt;%26lt;"Enter your age:";


cin%26gt;%26gt;age;


if(age%26lt;16)


cout%26lt;%26lt;"you are highschool.....";


cout%26lt;%26lt;endl;


else(age%26gt;16)


cout%26lt;%26lt;"Maybe you are college";


cout%26lt;%26lt;endl;


return 0;


}

About c++ (if and else)?
# include %26lt;iostream.h%26gt;


int main ()


{


// conditional statement


int age;


cout%26lt;%26lt;"Enter your age:";


cin%26gt;%26gt;age;


if(age%26lt;16){


cout%26lt;%26lt;"you are highschool.....";


cout%26lt;%26lt;endl;


}else{


cout%26lt;%26lt;"Maybe you are college";


cout%26lt;%26lt;endl;


}


return 0;


}








you dont need the else(age %26gt; 16), else means its going to be executed if the if statement is false and also you need





if(condition){





}else{





};








the { } are necessary is you have more than one line of code between if and else





if( condition)


one line


else


one line





is legal
Reply:You forgot the braces.





A if else statement's sytax is


if()


{





}else{





}





# include %26lt;iostream.h%26gt;


int main ()


{


// conditional statement


int age;


cout%26lt;%26lt;"Enter your age:";


cin%26gt;%26gt;age;


if(age%26lt;16)


{


cout%26lt;%26lt;"you are highschool.....";


cout%26lt;%26lt;endl;


}


elseif(age%26gt;16)


{


cout%26lt;%26lt;"Maybe you are college";


cout%26lt;%26lt;endl;


return 0;


}
Reply:if statements go like this





if (some conditional)


some expression


else


some expression





remember that to group a collection of expressions you need to put in { and }





so to make a BLOCK of code





if (some condition)


{


some code


some more code


}


else


{


even more code


a lot more code


}
Reply:You have two statements in the if block without curly braces.





if(age%26lt;16)


cout%26lt;%26lt;"you are highschool.....";


cout%26lt;%26lt;endl;


else(age%26gt;16)


cout%26lt;%26lt;"Maybe you are college";


cout%26lt;%26lt;endl;





should be:





if(age%26lt;16)


{


cout%26lt;%26lt;"you are highschool.....";


cout%26lt;%26lt;endl;


}


else(age%26gt;16)


{


cout%26lt;%26lt;"Maybe you are college";


cout%26lt;%26lt;endl;


}
Reply:try this:





int main ()


{


// conditional statement


int age;


cout%26lt;%26lt;"Enter your age:";


cin%26gt;%26gt;age;


if(age%26lt;16)


cout%26lt;%26lt;"you are highschool.....";


cout%26lt;%26lt;endl;


else


if(age%26gt;16)


cout%26lt;%26lt;"Maybe you are college";


cout%26lt;%26lt;endl;


return 0;


}
Reply:one line expression:





if()


... ;


else


... ;





more than one line expression:





if(){


... ;


... ;


}


else{


... ;


... ;


}








if(){


... ;


... ;


}


else if(){


... ;


... ;


}


I need help with a c++ program please?

im getting some errors in my program that i dont know how to fix help please


ill post the program first and then the errors





#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;


using namespace std;


void tally(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;


int p1,p2,vp1,vp2,s1,s2,t1,t2,j1,j...








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 help with a c++ program please?
I have never used C++, but just looking at the code, like in mathematics, it looks like you my be missing some brackets- like "}}}}" for the other side of your program code for your variables int p1,p2,vp1,vp2,s1,s2,t1,t2,j1, j.., and so on. Just like in Excel, mathematics, or any other program, your variables, Boolean logic, and other definitions will not work unless what you plug into the formulas follow certain procedures like Multiplication, Division, Addition, and Subtraction. I believe what you have at the top is what is called the Pseudo code (which describes what the program should do so this is only relevant to other programmers who want to know what your program should do).
Reply:What compiler you using with? it didn't show the line number where the error is, that's hard for you and us to know where is the error located.
Reply:What language is this???
Reply:i'm sick of that, why not u ask with the teacher.

magnolia

Trying to finish compute coin C++ program?

/***************************************...


* Program Name: Lab 12


* Author: Daniel Carr


* Date: 12 December 2007


* Course/Section: CSC-110-001


* Program Description: This program will tell what coins to give out for


* any amount of change from 1 cent to 99 cents.


*


*


**************************************...





/************************** Compiler Directives **********************/





#include %26lt;iostream%26gt;





using namespace std;





/*********************** Global Data Declarations ********************/





//None in this program.





/************************** Function Prototypes **********************/





//Sorts two numbers in ascending order


void input(int money);


int Calculatechange(int change);





int Calculatechange(int change);





void computecoin ( int%26amp; amount,int%26amp; coins, int denomiation);


void Output(int change, int quarters, int dimes, int nickles, int pennies);











/*************************************...


* Function Name: main


* Author: Daniel Carr


* Date: 8 November 2007


* Function Description:


*


* Pseudocode:


* Level 0


* -------


*


*


*


* Level 1


* -------


*


**************************************...





int main()


{


//Local variables


int money; //First user-entered number


int change; //Second user-entered number





/*************** Begin main Function Executables *****************/


//Variables





int quarters;


int dimes;


int pennies;


int nickles;








//Call function input


input(money);


//Call the function calculate


Calculatechange(change);


//Call function output


Output( change, quarters, dimes, nickles, pennies);














//Indicate to OS successful termination of program


return 0;





} //End main





/*************************************...


* Function Name: Input Money


* Author: Daniel Carr


* Date: 8 November 2007


* Function Description:


*


*


* Pseudocode:


* Level 0


* -------


* Enter amount of Money


*


* Level 1


* -------


* Enter amount of money


* Display "Enter amount of money (1-99)---%26gt;"


* Input Money


*


**************************************...


void input(int money)





//void inputmoney(float%26amp; x, //INOUT: First number to be sorted


// float%26amp; y) //INOUT: Second number to be sorted


{


//Local variables





cout %26lt;%26lt; "Enter amount of money (1-99)---%26gt;: ";


cin %26gt;%26gt; money;














} //End Input money





/*************************************...


* Function Name: Calculate Change


* Author: Daniel Carr


* Date: 8 November 2007


* Function Description: Calculate quarters, dimes, nickles, and pennies


*


*


* Pseudocode:


* Level 0


* -------


* Calculate Change


*


* Level 1


* -------


* Calcualte change


* Compute Quarters


* Compute Dimes


* Compute Nicklets and Pennies


* Level 2


* -------


* Compute Quarters


* Compute coin with Quarters, Change, and Q


* Compute Dimes


* Compute coin with Dimes, Change, and D


* Compute Nickles and Pennies


* Compute coin with Nickes Change and N,


*


*


*


*


*


*


*


**************************************...


int Calculatechange(int change)


{


//constants


const int q=25;


const int d=10;


const int n=5;


//arguements


int amount;


int denomiation;


int coins;





//Call the function computer coin


computecoin(amount,coins,denomiation)...











}





/*************************************...


* Function Name: Compute Coin


* Author: Daniel Carr


* Date: 8 November 2007


* Function Description: compute number of coin and amount left


*


*


* Pseudocode:


* level 0


* -------


* Compute number of coins and amount left


* level 1


* -------


* Compute number of coins and amount left


* coin=amount/demination


* amount = amount%demination


*


**************************************...


void computecoin ( int%26amp; amount,int%26amp; coins, int denomiation)


{


//Constistants





//Variables





//Arguements








coins = amount/denomiation;


amount= amount%denomiation;


amount %=denomiation;





























}





/*************************************...


* Function Name: Display Change


* Author: Daniel Carr


* Date: 8 November 2007


* Function Description: compute number of coin and amount left


*


*


* Pseudocode:


* level 0


* -------


* Display Change


* level 1


* -------


* Display Change


* display Change%26amp; "coints can be given" EOL


* display "-----------------------" EOL


* display quarters %26amp; "Quarter(s)" EOL


* display dimes %26amp; "Dimes(s)" EOL


* display nickles %26amp; "Nickles(s) EOL


* display pennies %26amp; "Pennie(s) EOL


*


**************************************...





void Output(int change, int quarters, int dimes, int nickles, int pennies)


{


//Aruments





//Consistants


//Variables





cout %26lt;%26lt; change %26lt;%26lt; "coins can be given" %26lt;%26lt; endl;


cout %26lt;%26lt; "-------------------------------------" %26lt;%26lt; endl;


cout %26lt;%26lt; quarters %26lt;%26lt; "quarter(s)" %26lt;%26lt; endl;


cout %26lt;%26lt; dimes %26lt;%26lt; "dimes(s)" %26lt;%26lt; endl;


cout %26lt;%26lt; nickles %26lt;%26lt; "Nickle(s) %26lt;%26lt; endl;


cout %26lt;%26lt; pennies %26lt;%26lt; "Pennie(s) %26lt;%26lt; endl;




















;}

Trying to finish compute coin C++ program?
U r missing quotes "...


"Nickle(s)"


"Pennie(s)"
Reply:Been through it a couple of times and the code seems ok but not sure about the way you've done your comments and pseudo code. C2001 is newline in constant %26gt;%26gt;





A string constant cannot be continued on a second line unless you do the following:





End the first line with a backslash.


Close the string on the first line with a double quotation mark and open the string on the next line with another double quotation mark.





http://msdn2.microsoft.com/en-us/library...





Can only assume the compiler is mistaking rems and pseudo for real code.


What am i doing wrong with my C++ program?

#include %26lt;iostream.h%26gt;


int main() {


int num, num2,avg, numValues, numValues2;


double maxNum, minNum;


int sum = 0;





numValues2 = 1;


do{


cout %26lt;%26lt; "Enter a value in the range of 5 to 10: ";


cin %26gt;%26gt; num2;


if(num2 %26lt;= 5 || num2 %26gt;= 10)


cout %26lt;%26lt; "This value is not in the proper range please try again" %26lt;%26lt; endl;


numValues2++;


}while (numValues2 %26gt; 0 %26amp;%26amp; numValues2 %26lt; 5);





maxNum = num;


minNum = num;


numValues = 1;


do {


cout %26lt;%26lt; "Enter number" %26lt;%26lt; numValues %26lt;%26lt; ": " ;


cin %26gt;%26gt; num;


if (num %26gt; maxNum)


maxNum = num;


if (num %26lt; minNum)


minNum = num;


sum+= num;


numValues++;


avg = sum / numValues;


}while (numValues %26gt; 0 %26amp;%26amp; numValues %26lt; 8);


cout %26lt;%26lt; endl;


cout %26lt;%26lt; "The Sum is: " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The Average is: " %26lt;%26lt; avg %26lt;%26lt; endl;


cout %26lt;%26lt; "The largest number is: " %26lt;%26lt; maxNum %26lt;%26lt; endl;


cout %26lt;%26lt; "The smallest number is: " %26lt;%26lt; minNum %26lt;%26lt; endl;





return 0;


}

What am i doing wrong with my C++ program?
Well from what I see the largest number is right.





I cant see why the smallest number isn't right and i dont know what you mean by stopping the loop.





Edit:


I found it change





int num, num2,avg, numValues, numValues2;


double maxNum, minNum;


to





int num2, avg, numValues, numValues2;


double num, maxNum, minNum;
Reply:Always assign default values to your variables. Not assigning default values to your variables is what's causing problems.





Another thing, when you know how many times users are going to input numbers then use a for-loop not a do-while or a while loop.





Also, num, sum and avg need to be doubles. Here is a cleaner version of your code:





#include %26lt;iostream.h%26gt;


int main()


{


int numValues = 0;


double sum = 0;


double currentNum = 0;


double maxNum = 0.0;


double minNum = 0.0;





do{


cout %26lt;%26lt; "Enter a value in the range of 5 to 10: ";


cin %26gt;%26gt; numValues ;


if(numValues %26lt;= 5 || numValues %26gt;= 10)


cout %26lt;%26lt; "This value is not in the proper range please try again" %26lt;%26lt; endl;


}while (numValues %26lt;= 5 || numValues %26gt;= 10);








for(int i = 0; i %26lt; numValues; i++)


{


cout %26lt;%26lt; "Enter number" %26lt;%26lt; (i + 1) %26lt;%26lt; ": " ;


cin %26gt;%26gt; currentNum;


if(i == 0)


maxNum = minNum = currentNum;


else


{


if (currentNum%26gt; maxNum)


maxNum = currentNum;


else if (currentNum%26lt; minNum)


minNum = currentNum;


}


sum+= currentNum;


}





avg = sum / (double) numValues;


cout %26lt;%26lt; endl;


cout %26lt;%26lt; "The Sum is: " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The Average is: " %26lt;%26lt; avg %26lt;%26lt; endl;


cout %26lt;%26lt; "The largest number is: " %26lt;%26lt; maxNum %26lt;%26lt; endl;


cout %26lt;%26lt; "The smallest number is: " %26lt;%26lt; minNum %26lt;%26lt; endl;





return 0;


}
Reply:I found few minor mistakes in your program.





They are wrong declaration of variables.


Using do - while instead of while or for loop


arg variable should be outside the loop


etc..





so i have typed your program in my own style





Hope you will like it. If you have any problem call me at m_gopi_m@yahoo.co.in





#include%26lt;iostream.h%26gt;


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int i,j,n,num,max,min;


double sum=0,avg;





clrscr();


cout%26lt;%26lt;"\n\n Enter the number of numbers: ";


cin%26gt;%26gt;n;





cout%26lt;%26lt;"\n Enter the numbers,\n\n";


cout%26lt;%26lt;" num 1 : ";


cin%26gt;%26gt;num;





max=num;


min=num;


sum=num;





for(i=1;i%26lt;n;i++)


{


if(num%26gt;max)


max=num;


else if(num%26lt;min)


min=num;





sum+=num;





cout%26lt;%26lt;" num "%26lt;%26lt;i+1%26lt;%26lt;" : ";


cin%26gt;%26gt;num;


}


avg=sum/n;





clrscr();


cout%26lt;%26lt;"\n\n\n\n\t\t\t\tRESULTS " ;


cout%26lt;%26lt;"\n\n The smallest number is\t\t"%26lt;%26lt;min;


cout%26lt;%26lt;"\n The biggest number is\t\t"%26lt;%26lt;max;


cout%26lt;%26lt;"\n The sum of the given number is "%26lt;%26lt;sum;


cout%26lt;%26lt;"\n Their average is\t\t"%26lt;%26lt;avg;


getch();


}
Reply:I would love to help you, but from your code it isn't clear what you're trying to do --I'm afraid your code is just THAT messy...





Note that num should probably be a double, and avg shouldn't be calculated inside the loop, but outside of it. That's just two major bugs that I see, but please describe what you want your program to do in English so I can give you a complete answer.