Monday, May 24, 2010

C++ maxtrix?

I have to create a gradebook that reads in any amount of students and what their grades are. The user can add more to the matrix if they need another column of grades. I am having a problem with my addGrades function and need help on what is worng and how I can fix it. Here is the function for my "Add Grades" function:





void AddGrades(vector%26lt;String%26gt; %26amp;name, TTTBoard %26amp;Board)


{








int Row = Board.numrows() - 1;


int Col = Board.numcols() - 1;





//Board.resize(Col,Row);


//for(int j=0;j%26lt;= Col;j++){





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


cout%26lt;%26lt;"Please enter a grade for "%26lt;%26lt;name[i]%26lt;%26lt;" ";


cin%26gt;%26gt;Board[Row][i]; //j,i ?


Row++;


}//end for





//}//end for











}//end fun.





//Thanks.

C++ maxtrix?
void AddGrades(vector%26lt;String%26gt; %26amp;name, TTTBoard %26amp;Board)


{


int Row = Board.numrows() - 1;





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


cout%26lt;%26lt;"Please enter a grade for "%26lt;%26lt;Name[i]%26lt;%26lt;" ";


cin%26gt;%26gt;Board[i];


}//end for





}//end fun.
Reply:This if for a console app fight? Hence the cout statements? If so, the cin statement only needs to be seperated by commas.... and why are you using { instead of ; for the endings of a few of your lines of code?

floral design

Saving the enter key in array..c++??

cin.get cant do that nor can cin%26gt;%26gt;


so how do i do it??

Saving the enter key in array..c++??
cin%26gt;%26gt;"here witte the arry as you predifine"





this can save your arry.


C++ heap problem (new fuction)?

How do I create an array of Employees at run time? The size of the array is entered by the user.





I tried...


cin %26gt;%26gt; num;


Employee[] emp = new Employee[num];





but its wrong.

C++ heap problem (new fuction)?
Does it need to be an array? Why don't you use a vector?





this is what vectors are used for.
Reply:This should work fine:





Employee* pemp = new Employee[num];





Then pemp[0] to get the first element, pemp[1], etc.





Ensure you never read past pemp[num-1] as that is not your memory.
Reply:1. use a array of character pointers (string)


2. then create a buffer say buffer[255] that will accept user input


3. get the length of the string entered


4. then use malloc to allocate storage for the string


5. copy the entered string to the storage allocated in 4


6. do steps 4 to 5 for every new employee
Reply:You need to initialize your Employee (class) to zero (0) before you create the array. I believe this is called the "default constructor". Also, when you the function has run its course, you must destroy the array with: delete Employee; . Otherwise, the memory (heap) manager will cause a run time error. Remember, C++ does not have automatic garbage collection, though Java does.


C++ Strings?

I have to take in a a string and scramble it 5 times, using string methods. I have to separate it using 2 radnom num's, and into three parts. It won't execute for some reason. Any help will be appreciated. Here's the code:


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter a phrase to shuffle: ";


getline(cin,phrase_input);





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


{


beg = phrase_new.substr(0,random_one);


mid = phrase_new.substr(random_one,random_two - random_one);


endphr = phrase_new.substr(random_two,phr_len - random_two);





shuffled_phrased+= endphr;


shuffled_phrased+= beg;


shuffled_phrased+= mid;





}


cout%26lt;%26lt;shuffled_phrased;

C++ Strings?
first, I assume that somewhere you are copying phrase_input into phrase_new somewhere and just didnt show the code.





second, make sure your random_one and random_two are not greater than the length of the string phrase_new.





third, to loop 5 times, your "for" loop should be "for(int i = 0; i %26lt; 5; i++)"





fourth, you need to copy shuffled_phrased back into phrase_new at the end of your loop, so you can repeat the operations on it again.
Reply:Do you know how to use a debugger?


If not, I suggest you learn.





A debugger will help you to pinpoint a problem to a specific line of code. Learn to step through the code and verify it is doing what it is supposed to be doing.





Learn to use breakpoints and the call stack.


C++ Vectors?

The instructions for this problem are:


" Write a complete program that reads in 100 double values from standard input (keyboard) into a vector. Then output all 100 values to standard output (terminal) in the same order they were read in with one space between each value."





I do not need to worry about prompting the user with instructions.





The code I have so far is:





#include %26lt;iostream%26gt;


#include %26lt;vector%26gt;





int main()


{


vector%26lt;double%26gt; values(99);





while( int i = 0 ; i %26lt;= 99 ; i++ )


{


cin %26lt;%26lt; vector[i];


}





for ( int i = 1 ; i %26lt; values.size()-1 ; i++ )


{


cout %26lt;%26lt; vector[i] %26lt;%26lt; " " ;


}





return 0;


}





Could someone please tell me what I am doing wrong? Thanks in advance.

C++ Vectors?
Here is correct code based on your code:





#include %26lt;iostream%26gt;


#include %26lt;vector%26gt;





using namespace std;


const static int vec_sz = 100;


vector%26lt;double%26gt; values(vec_sz);





int main( int ac, char * av[] )


{





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


{


cin %26gt;%26gt; values[i];


}





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


{


cout %26lt;%26lt; values[i] %26lt;%26lt; " " ;


}


return 0;


}
Reply:cin %26gt;%26gt; values[i]; (not %26lt;%26lt;)





and cout %26lt;%26lt; values[i], not vector[i]





if the cin doesn't work (vector::operator[] returns a reference so it should in theory) use a temporary double variable first then assign that into the vector.

chelsea flower show

Can someone please tell me what's wrong with my C++ program?this program should show how many students earned

a specific score. it is not working because i type inn 72 and it shows 20. the array only has two 72 scores. then, if i want to type inn another score, i have to type it twice in order for the screen to show an output. this is my code:


#include %26lt;iostream%26gt;





using std::cout;


using std::cin;


using std::endl;





int main()


{


//declare array


int count = 0;


int searchFor = 0;


int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99,


97, 76, 73, 72, 56, 73, 72, 20, 86, 99};





//get input from user


while (searchFor != -1)


{


cout %26lt;%26lt; "Enter a score from 0 to 100: " ;


cin %26gt;%26gt; searchFor;





//search for score equal to the searchFor value


for (int x = 0; x %26lt; 20; x = x + 1)


if (scores[x] = searchFor)


count = count + 1;


//end if


//end for


//display cout


cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;





//get input from user


cout %26lt;%26lt; "Enter a score from 0 to 100: " ;


cin %26gt;%26gt; searchFor;


}//endwhile


return 0;


} //end of the searchArray function

Can someone please tell me what's wrong with my C++ program?this program should show how many students earned
There has to be == on the if statement








while (searchFor != -1)


{


// User enters a score


cout %26lt;%26lt; "Enter a score from 0 to 100: " ;


cin %26gt;%26gt; searchFor;





// If score is in the array, then add to count


for (int x = 0; x %26lt; 20; x++)


{


if (scores[x] == searchFor)


count++;


}





// Display count


cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;





}





return 0;


}//end of the searchArray function


How do I get rid of the newline character in C++ using ignore?

I have this class structure, I'm trying to read in strings.


for (int count = 0; count %26lt; num; count++) {


cfile %26gt;%26gt; library[count].name.last;


cfile %26gt;%26gt; library[count].name.first;


getline(cfile, library[count].book.title);


cfile %26gt;%26gt; library[count].book.pages;


getline(cfile, library[count].book.genre);


getline(cfile, library[count].book.other.publisher);


cfile %26gt;%26gt; library[count].book.other.year;


}


And I asked this guy and he said:


After you cin%26gt;%26gt; name.first and name.last, you will have read "Jk" and "Rowling", respectively, but you've left the newline in the stream. The call to getline will receive an empty line, and when you try to convert that empty string to a number, you get something completely random like 211400.





How do I get rid of the newline character, something to do with cin.ignore????

How do I get rid of the newline character in C++ using ignore?
The guy you asked answered correctly. When reading from an instream object using %26gt;%26gt;, you have to be careful about newlines being left behind.





I could type up the solution here, but someone else has already done so. I'll give you the link: http://www.daniweb.com/tutorials/tutoria...





I'll summarize the two solutions you have. The first, and recommended for beginners to C++, is to avoid cin and use getline instead. The other way is cin.ignore(numeric_limits%26lt;streamsize%26gt;::m... '\n').





Please read the entire article. It is technically accurate as well.





In the future, use Google to find out answers. There's good resources at "C++ FAQ", cplusplus.com , cppreference.com.
Reply:cin.ignore('\n');


Is there an efficient way to toss binary data in C++?

Below if the code I'm using. I wonder if anyone knows a faster way to do what I'm doing, which is taking in a file by reading its data byte by byte, and throwing out every other byte (so the final output is 1/2 the original).





I find that that way I'm doing it is really slow. Is there a way to do this faster? I think that reading the file in at once might speed things up so that cin doesn't have to be called over and over, but I don't know how to go about doing that.











#include%26lt;iostream%26gt;





using namespace std;





main()


{int i=0;


char ch;





while(cin.get(ch))


{if(i == 1)


{cout %26lt;%26lt; ch;


i = 0;


}


else if(i == 0)


{i = 1;


}





else {exit(-1);}





}





}

Is there an efficient way to toss binary data in C++?
You'd be better off reading the entire file into a dynamically allocated array to begin with, then using pointer arithmetic to remove the unnecessary bytes. File accesses are super slow, but if you read the entire thing first you can do it by reading in much bigger chunks and then just iterate over the list of read in data and skip every other byte.


Validating crap in c++?

i'm trying to validate numbers as I enter them in my program. can anyone tell me whats wrong with this?





cout %26lt;%26lt; "\nHow many whole numbers (up to 20) would you like to organize? ";


cin %26gt;%26gt; numberNum;


if (numberNum != '0','1','2','3','4','5','6','7','8','9' )


{


cout %26lt;%26lt; "Please enter a valid number: " %26lt;%26lt; endl;


}





everytime i run the program, the message pops up. its as if the compiler sees the valids "1,2,3..." as invalid. any suggestions?

Validating crap in c++?
why don't u try this


cout %26lt;%26lt; "\nHow many whole numbers (up to 20) would you like to organize? ";


cin %26gt;%26gt; numberNum;


if (numberNum %26gt; 20 || numberNum %26lt;0)


{


cout %26lt;%26lt; "Please enter a valid number: " %26lt;%26lt; endl;


cin %26gt;%26gt; numberNum


}
Reply:The comma is a valid C++ operator, but it doesn't do what you're thinking it does. The comma evaluates the expression on its left, discarding the result, then returns the type and value of the expression on the right. Also, you're making your life difficult. For 0-20, it's difficult, but what if you wanted the numbers 0-100 to be valid?! Typing them all out would get insane, and a phone number, a MILLION possible numbers you don't want to type!





use if(numberNum %26gt;= 0 %26amp;%26amp; numbernNum%26lt;=20)





now if you want to expand it to a million all you have to change it to would be





if(numberNum %26gt;= 0 %26amp;%26amp; numberNum%26lt;= 10000000)





(you'd also have to make it an integer, but a search of answers will tell you how to do that).
Reply:instead you need this...





if (numberNum != '0' %26amp;%26amp; numberNum != '1' %26amp;%26amp; numberNum != '2') //etc





Perhaps a better way to ensure the validity of the input is to get it as a string using getline, then pass it to a function like atoi() and check for an error.
Reply:This only checks 0. You need :


if ((numberNum %26gt;-1) %26amp;%26amp; (numberNum %26lt;20)


{//routine for a match


}else


{//routine for no match


}
Reply:"numberNum != '0','1','2','3','4','5','6','7'..." is not valid.





You would need to do this: "numberNum != '0' %26amp;%26amp; numberNum != '1' %26amp;%26amp; numberNum != '2'..."





Otherwise, instead of using cin, do this:





int numberNum;


scanf(%d , %26amp;numberNum ); //read in the value as an integer


if( numberNum %26lt; 0 || numberNum %26gt; 20) {


....


}

apple

Why doesnt my C++ program work? Help!?

ok so im supposed to write a program that calculates n! using functinos, this is what i have so far.





#include %26lt;iostream%26gt;


using namespace std;





int fact(int n, Int factor, int i)


{


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


factor = factor * i;


return factor;


}





int main ()


{


int n, factor, i;


cout%26lt;%26lt;"Enter a number"%26lt;%26lt;"\n";


cin%26gt;%26gt;n;


factor = fact(n,factor,i);


cout%26lt;%26lt;factor;





return 0;


}





i copile it and it checks out but my answer is always zero. Can anyone see what i did wrong?

Why doesnt my C++ program work? Help!?
%26gt; i did but all i got then was an error message, NO feaken


%26gt; WAY!!!! I did before and all i got was an error message


%26gt; i did it just now again and it worked, wtf!!! My comp is


%26gt; messing with me, (Argg)





Welcome to the world of programming! It sucks! You're gonna love it...
Reply:try initializing factor to 1 before calling the function.
Reply:Change the line 'int n,factor,i;' to this:


int n,i;


unsigned long factor=1;





In your original program, 'factor' was not initialized, so the initial value could have been anything. Also, by using an unsigned long data type for 'factor', the program will allow a number up to 4294967295.


You had 'factor' as an integer, so the maximum number it can hold is 32767.
Reply:hey man what are you doing its all wrong let me write that for you. It goes like this........





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


int fact(int n)


{ int factorial=1;


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


{


factorial=factorial*i;


}


return factorial;


}





int main()


{


int n;


cin%26gt;%26gt;n;


int factorial=fact(n);


cout%26lt;%26lt;factorial;


return 0


}





hope it helps.


Cannot get this C++ program to compile using bounds.. help plz!?

Okay, this program consists of repeated calls to 2 functions to compute the sum and product of all integers between two bounds. For my example, the user is imputing 3 for lower bound, and 7 for upper bound.





I cannot get this program to compile, and suggestions would be great! I will need to add a count controlled loop to this program later, I will just have to try to figure that out once i can get this portion to work though.





My code so far:





#include %26lt;iostream%26gt;


using namespace std;





int main();


int sum(int lower, int upper){


cout %26lt;%26lt; " Enter a lower and upper bound: ";


cin %26gt;%26gt; lower %26gt;%26gt; upper;





int total = 0; //holds the total that will be returned


for(int i = lower; i %26lt;= upper; i++); //calculate the total


total += i; //returns the total, which is the sum 25 (3 + 4 + 5 + 6 + 7)


return total;


}











Thanks in advance!!

Cannot get this C++ program to compile using bounds.. help plz!?
First you ask for product and now you are asking about sum,. It would be better if you do it yourself
Reply:this should work





#include %26lt;iostream%26gt;





using namespace std;





int getNums(int %26amp;lower, int %26amp;upper);


int sum(int, int);





int main(){


int upper=0, lower=0, total=0;





getNums(lower, upper);


total = sum(upper, lower);


return 0;


}





int sum(int upper, int lower){


int total=0;


for(int i=lower; i%26lt;=upper; i++)


i+=total;





return total;


}





int sum(int %26amp;lower, int %26amp;upper){


cout %26lt;%26lt; "Type a lower and upper bounds:\n";


cin %26gt;%26gt; lower;


cin %26gt;%26gt; upper;


return 0;


}
Reply:In C++, to get this to compile you have a bad arrangement of braces that must be remedied. I'll show them in a better light below.





#include %26lt;iostream%26gt;


using namespace std;





int sum(void){//definition of function. { required.





int lower, upper;


cout %26lt;%26lt; " Enter a lower and upper bound: ";


cin %26gt;%26gt; lower %26gt;%26gt; upper;


int total = 0; //holds the total that will be //returned


for(int i = lower; i %26lt;= upper; i++){ //calculate the //total


total += i; //returns the total, which is the sum //25 (3 + 4 + 5 + 6 + 7)


}//end of for loop.


return total;


} // end of function sum.





int main(){


cout %26lt;%26lt; sum();


return 0;


}// end of main.





I've got it so it will compile, so start in from there and reread any material you've got on what demarcates a function in C/C++. I hope this helps. Pivy.


Syntax error in c++ program?

while () (ch=cin.get) ! =\n

Syntax error in c++ program?
while ( (ch = cin.get()) != '\n') { }
Reply:while ( (ch = cin.get) != \n) { }
Reply:while () (ch=cin.get) ! ="\n"


What is wrong with this C++ program?

#include%26lt;iostream%26gt;


using namespace std;





int main()


{


int numCount, total;


double average;





cout %26lt;%26lt; "How many numbers do you want to average? ";


cin %26gt;%26gt; numCount;


for (int count = 0; count %26lt; numCount; count++)


{


int num;


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


cin %26gt;%26gt; num;


total += num;


}


average = total / numCount;


cout %26lt;%26lt; "The average is " %26lt;%26lt; average %26lt;%26lt; endl;


return 0;


}

What is wrong with this C++ program?
dude int total get initialized to garbage value..


so use


int numCount,total=0;


this should giv u the correct answer





#include%26lt;iostream%26gt;


using namespace std;





int main()


{


int numCount, total=0;


double average;





cout %26lt;%26lt; "How many numbers do you want to average? ";


cin %26gt;%26gt; numCount;


for (int count = 0; count %26lt; numCount; count++)


{


int num;


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


cin %26gt;%26gt; num;


total += num;


}


average = total / numCount;


cout %26lt;%26lt; "The average is " %26lt;%26lt; average %26lt;%26lt; endl;


return 0;


}
Reply:total/numCount is integer devided by integer wil result in fraction part truncated. Also all vaiables need to be initialized to 0.





Two Solutions


1. Use datatype casting: i.e., You can replace


average = total/numCount ;


with


average=(double)total / (double) numcount;





2. Initialize numcount and Total as double/float datatype


instead of integer.
Reply:It looks ok to me. The only thing I would suggest is initializing your variables to something so you know you don't have junk in them. So set numCount and total to 0 at the top and also set average to 0.0 and don't forget to set num = 0 in your for loop as well. I'm not sure exactly what the problem is or if there is one since you didn't mention a specific error.
Reply:Try placing the "int num;" outside of the for.





for total, make it "total = total + num;"





Remember to close everything, even "for" with a ";"





If this doesn't fix it, can you say what exactly the problem is when it runs?





It's been a long time since C++, sorry if I'm wrong.

augustifolia

Sos, plz solve the errors c++(core)?

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


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


main()


{


float a,b,sign;


cout%26lt;%26lt;"enter two numbers";


cin%26gt;%26gt;a%26gt;%26gt;b;


cout%26lt;%26lt;"choose a sign";


cin%26gt;%26gt;sign;


if (sign==1)


{sign=a*b;


cout%26lt;%26lt;sign;}


if (sign==2)


{sign=a+b;


cout%26lt;%26lt;sign;}


if (sign==3)


{sign=a/b;


cout%26lt;%26lt;sign;}


if (sign==4)


{sign=a-b;


cout%26lt;%26lt;sign;}


getch()


}


}

Sos, plz solve the errors c++(core)?
what kind of math is that??????????


ummmmmmm.................................


dont know sorry
Reply:#include%26lt;iostream.h%26gt;


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


main()


{


float a,b,sign;


cout%26lt;%26lt;"enter two numbers";


cin%26gt;%26gt;a%26gt;%26gt;b;


cout%26lt;%26lt;"choose a sign";


cin%26gt;%26gt;sign;


if (sign==1)


{sign=a*b;


cout%26lt;%26lt;sign;}


if (sign==2)


{sign=a+b;


cout%26lt;%26lt;sign;}


if (sign==3)


{sign=a/b;


cout%26lt;%26lt;sign;}


if (sign==4)


{sign=a-b;


cout%26lt;%26lt;sign;}


cout%26lt;%26lt;flush;


getch();


}








// Note : you can remove cout%26lt;%26lt;flush; it forces the cout to write on the screen before the pause caused by getch();
Reply:Unless you can be more specific regarding an error (logical) ... as I do not understand the intent of the code other than the obvious ... there are two syntax errors :





(1) Missing semicolon after "getch()"





(2) An excess brace after the line mentioned above.





I would also clean up the code by using a simple switch statement and a few other minor adjustments, as follows :





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


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





void main()


{


float a, b, result;


int operator;





cout %26lt;%26lt; "Enter two numbers :";


cin %26gt;%26gt; a;


cin %26gt;%26gt; b;





cout %26lt;%26lt; "Select an operator.";


cout %26lt;%26lt; "1. Multiplication";


cout %26lt;%26lt; "2. Addition";


cout %26lt;%26lt; "3. Division";


cout %26lt;%26lt; "4. Subtraction";


cin %26gt;%26gt; operator;





switch (operator)


{


case 1 :


result = (a * b);


break;





case 2 :


result = (a + b);


break;





case 3 :


result = (a / b);


break;





case 4 :


result = (a - b);


break;


}





cout %26lt;%26lt; "Result = " %26lt;%26lt; result;





getch();


}


I have got a little broblem when i compile this c++ code could u help me?

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


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


#define close


struct data{


char name[30];


int year;


}person [close];


int main()


{


char buffer [30];


int n;


cout%26lt;%26lt;"How many persons do u want to enter?";


cin.getline (buffer,30); close=atoi(buffer);


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


{


cout%26lt;%26lt;"Enter the name of the person: ";


cin.getline (person[n].name,30);


cout%26lt;%26lt;"How old is "%26lt;%26lt;person[n].name%26lt;%26lt;" ?";


cin.getline (buffer,30);person[n].year=atoi(buffer);


}


cout%26lt;%26lt;"The persons data u entered are as follows:\n";


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


{


cout%26lt;%26lt;person[n].name%26lt;%26lt;" is "%26lt;%26lt;person[n].year%26lt;%26lt;" year(s) old! \n";


}





return main();


}





i want the output to be like this:


how many persons do u eant to enter?


3


enter the name of the person: ben


how old is ben? 19


enter the name of the person: catherine


how old is catherine? 17


enter the name of the person: sophia


how old is sophia? 22


the persons data u entered are as follows:


ben is 19 years old


catherine is 17 years old


sophia is 22 year

I have got a little broblem when i compile this c++ code could u help me?
change three things


#define close


to


#define close 5





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


to


for (n=0;n%26lt;close;n++)





and


return main();


to


return 0;





then the program works
Reply:One major flaw that probably causes you error is


#define close


has no value also, it's constant you wont be able to change it


unless





#define close(x) x %26gt; 0 ? x : 0





then in your code you can do something like


close(atoi(buffer))


note: This will work like a function with a return value so you'll have to assign it





If you want user input then don't use a #define close


declare it local or global if you want because when you use #define it'll be constant and won't change
Reply:First, you want to change:


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


to:


for (n=0;n%26lt;close;n++)


Otherwise, it will ask for one less than what you wanted.





Also, you want to change:


return main();


to:


return true;


Otherwise, it will restart as soon as it finishes. (If you want it to automatically run again after it finishes printing, use a while loop like this:





....


int main()


{


while(true)


{


char buffer [30];


....


cout%26lt;%26lt;person[n].name%26lt;%26lt;" is "%26lt;%26lt;person[n].year%26lt;%26lt;" year(s) old! \n";


}


}


return true;


}





it will give the same result, but this is a much better programming style.


Can someone please check my C++ program? please!!!?

ok. This program should display the string "Dog" when the animal variable catains the letter D (in any case); otherwise, it should display the string "Cat".





However, when i run it, it only displays the string "Cat" no matter what letter i type in it. WHY? this is my code:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::string;





int main()


{


//declare variable


char animal = ' ';


string ID = "";





//enter input


cout %26lt;%26lt; "Enter an animal ID: ";


cin %26gt;%26gt; ID;








//display ID


if (animal == 'D' || animal == 'd')


cout %26lt;%26lt; "Dog" %26lt;%26lt; endl;


else


cout %26lt;%26lt; "Cat" %26lt;%26lt; endl;


//end if





return 0;


} //end of main function

Can someone please check my C++ program? please!!!?
You never put anything in animal?


Your if needs to be checking ID == D or ID == d not animal. where does animal get its value? So right now animal doesn't have anything in it, and thus null ==D is false and the else statement is fired.
Reply:I agree with the ones above that say animal is still empty.





You can try printing it out to verify:





I think you are trying to check the first letter of ID, but you never copy it to your animal variable. Add the following line after your cin line:





animal = ( ID . c_str ( ) ) [0] ;





This copies the char to animal.
Reply:not if animal





use ID == 'D' || ID == 'd'
Reply:You don't need ID (and you can't test it's value using == anyway). Change:





cin %26gt;%26gt; ID;





to:





cin %26gt;%26gt; animal;
Reply:It seems to me you have your input set to the variable ID and then your if statement is using the variable animal which you have never assigned a value to.





So since animal = "" then the else case is always true and you always get cat.





Change the line cin %26gt;%26gt; ID to cin %26gt;%26gt; animal
Reply://Display ID section...





you;re using the variable ID to hold what letter they enter...


then at the point where you are going to display the result you're checking the variable animal not ID





change that to:


if (ID == 'D' || ID == 'd')

nobile

What is wrong with this C++ syntax?

#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;








void convert(double numberSize, double current, string typeOF);


int main()


{


double number;


char measurement;





cout%26lt;%26lt; "Number\n%26gt;%26gt;";


cin %26gt;%26gt; number;


cout %26lt;%26lt; "Type\n%26gt;%26gt;";


cin %26gt;%26gt; measurement;





switch(measurement)


{


case "inches":


convert(0.083 333 333, number, "inches");


}


system ("pause");





return 0;


}


void convert(double numberSize, double current, string typeOF)


{


cout %26lt;%26lt; "Type: " %26lt;%26lt; typeOF %26lt;%26lt;


endl %26lt;%26lt; "Size: " %26lt;%26lt; numberSize * current;


}

What is wrong with this C++ syntax?
It looks like your variable "measurement" should be a character array instead of char. Then you use cin.getline to read it in. Finally you cannot use a switch statement with strings, so you would use an if statement.





Example:


char measurement[128];


std::cin.getline( measurement, 128);





std::string compareString;


compareString = measurement;





if(compareString.compare("inches") == 0)


{


// do stuff


}
Reply:Haven't done much C++ before but these lines:





cout%26lt;%26lt; "Number\n%26gt;%26gt;";


cout %26lt;%26lt; "Type\n%26gt;%26gt;";





should be,





cout%26lt;%26lt; "Number\n"%26gt;%26gt;;


cout %26lt;%26lt; "Type\n"%26gt;%26gt;;





The nesting of quotes is wrong at the moment.
Reply:Paraphrasing the compiler error in g++, "Case labels must be able to be reduced to an integer constant". "inches" is an array of chars. Your compiler probably should have given you an error.





If you use the 'char' type for your case labels, e.g. 'i', than it will work, however this limits you to single-letter input. If you really want to use strings, you can use a hash or a map to get an integer value from your known strings. It looks as if you're only altering a double (numberSize), so you can make a string to double map that will return the desired constant when you feed it a string, and scrap the switch. Something like:





struct CompStruct


{


bool operator()( const char* s1, const char* s2 ) const


{


return strcmp( s1, s2 ) %26lt; 0;


}


};





//inside main():





map%26lt;const char*, double, CompStruct%26gt; numberSizes;


numberSizes["inches"] = 0.083333333;


numberSizes["something"] = 0.123456789;


// etc.





string stringFromUser;


cin %26gt;%26gt; stringFromUser;


convert(numberSizes[stringFromUser], number, stringFromUser);








The syntax might be off, but I suppose it will work. Don't forget your error handling in case the key is not found.


Error in c++ program?

I'm trying to find the max of 10 numbers and I don't need to keep the number after I've checked to see if it is greater then the max. I'm going off the model my teacher gave me but it's not working and nothing I do helps.





int max=0;


int n1;


int i=0;


while(i %26lt; 10)


{


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


cin%26gt;%26gt;n1;


if (max%26lt;n1)


max=n1


i=i+1


}


cout%26lt;%26lt;"The maximum number is"%26lt;%26lt;max%26lt;%26lt;endl;





when I check I come up with


error: expected unqualified-id before awhilea


error: expected constructor, destructor, or type conversation before a%26lt;%26lt;a token

Error in c++ program?
You need to declare a main() function as an entry point, so the program knows where to start. ^_^





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


int main()


{


(your above code goes here)


}
Reply:int max=0;


int n1;


int i=0;


while(i %26lt; 10)


{


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


cin %26gt;%26gt; n1;


if (max %26lt; n1)


max = n1;


i=i+1;


}


cout %26lt;%26lt; "The maximum number is" %26lt;%26lt; max %26lt;%26lt;endl;





You had missed two ;


Translating a code from C++ to Java...?

I need some help putting this code into Java





#include %26lt;iostream%26gt;


using namespace std;





int main() {


int sales[9]= { 0 };


double gross=0, percent=0, salary=0;


int total=0;


int counter=0;


cout %26lt;%26lt; "For 5 peoples salary ranges, Enter the first gross" %26lt;%26lt; endl;


cin %26gt;%26gt; gross;


percent=gross*0.09;


salary=200+percent;


while (counter!=5) {


if (salary%26gt;=200 %26amp;%26amp; salary %26lt;= 299)


sales[0]++;


if (salary%26gt;=300 %26amp;%26amp; salary%26lt;=399)


sales[1]++;


if (salary%26gt;400 %26amp;%26amp; salary%26lt;=499)


sales[2]++;


if (salary%26gt;=500 %26amp;%26amp; salary%26lt;=599)


sales[3]++;


if (salary%26gt;=600 %26amp;%26amp; salary%26lt;=699)


sales[4]++;


if (salary%26gt;=700 %26amp;%26amp; salary%26lt;=799)


sales[5]++;


if (salary%26gt;=800 %26amp;%26amp; salary%26lt;=899)


sales[6]++;


if (salary%26gt;=900 %26amp;%26amp; salary%26lt;=999)


sales[7]++;


if (salary%26gt;=1000)


sales[8]++;


cout %26lt;%26lt; "Enter the next gross" %26lt;%26lt; endl;


cin %26gt;%26gt; gross;


counter++;


}


cout %26lt;%26lt; sales[0] %26lt;%26lt; " are in 200-299, " %26lt;%26lt; sales[1] %26lt;%26lt; " are in 300-399, " %26lt;%26lt; sales[2] %26lt;%26lt; " are in 400-499, " %26lt;%26lt; sales[3] %26lt;%26lt; " are in 500-599, " %26lt;%26lt; sales[4]

Translating a code from C++ to Java...?
Declarations of variables and main() as well as imports are also different. You can also clean up your if statements to make them more efficent (one comparison instead of two).





This is a sample class that I called stest (in stest.java) that will illustrate how to use the scanner class to read from stdin (I left out all error checking and it will allow multiple gross values to be placed on a single input line which will result in unneeded prompts, but this is just a sample). It also illustrates how you can make one test instead of two in each of your if statements.





import java.util.*;


import java.lang.*;


import java.io.*;





class stest


{


public static void main( String argv[] )


{


int sales[]; // declaration; allocation is later


String range_names[] = { "200-299", "300-399", "400-499", "500-599", "600-699", "700-799", "800-899", "900-999", "%26gt;1000" };


Scanner s; // input scanner object


int need = 5; // number of values needed


double percent = 0; // percentage commission maybe?


double salary = 0; // persons computed salary


double gross = 0; // gross sales maybe?


int idx; // index into sales





sales = new int[9]; // allocate the array (Id name this salarycount or something relative)





s = new Scanner( System.in ); // read from standard input (assume keyboard)


while( need %26gt; 0 )


{


System.out.printf( "enter gross sales for person %d: ", 6-need );


gross = s.nextInt( ); // get next int value from stdin (no error checking here)





percent = gross * 0.09; // duplication of your logic


salary = 200.0 + percent; // duplication of your logic





if( salary %26gt;= 1000 ) // working backwards needs just one


idx = 8; // comparison so more efficent


else


if( salary %26gt;= 900 )


idx = 7;


else


if( salary %26gt;= 800 )


idx = 6;


else


if( salary %26gt;= 700 )


idx = 5;


else


if( salary %26gt;= 600 )


idx = 4;


else


if( salary %26gt;= 500 )


idx = 3;


else


if( salary %26gt;= 400 )


idx = 2;


else


if( salary %26gt;= 300 )


idx = 1;


else


idx = 0;





sales[idx]++;





need--; // one less to do


}





for( idx = 0; idx %26lt; 9; idx++ )


System.out.printf( "salaries in the range of %s: %d\n", range_names[idx], sales[idx] );





}


}











==== output when run with random numbers ====


java stest


enter gross sales for person 1: 3000


enter gross sales for person 2: 7000


enter gross sales for person 3: 24000


enter gross sales for person 4: 1200


enter gross sales for person 5: 1680


salaries in the range of 200-299: 0


salaries in the range of 300-399: 2


salaries in the range of 400-499: 1


salaries in the range of 500-599: 0


salaries in the range of 600-699: 0


salaries in the range of 700-799: 0


salaries in the range of 800-899: 1


salaries in the range of 900-999: 0


salaries in the range of %26gt;1000: 1


Load and save feature c++?

//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

Load and save feature c++?
It seems like you aren't trying to store anything in an fstream at all even though you included it. Just initialize an fstream and write to the same place each time. The 49 you see is probably an uninitialized variable that happens to have a 49 in it. It will be something else later.

hibiscus

Help me in my c++ program?

could any one found the error ?





#include%26lt;iostream%26gt;


using namespace std;





double bank (double principle,double year ,double rate )


{


cout%26lt;%26lt;"enter ur principle , please \n";





cin%26gt;%26gt;principle;





cout%26lt;%26lt;"enter how many years u put this principle , please \n";





cin%26gt;%26gt;year;





rate= (principle +year*0.06);





return rate;





}


int main()


{


bank();





return 0;


}

Help me in my c++ program?
You are not passing any parameters/arguements while making a call to bank() ....


replace bank(); with bank(1000,2,3);


that ought do the trick
Reply:You don't need the parameter if you are asking for the values in the function your called from main is right you just need to take out the parameters from the function. Also if you want this code to be safe you need to make sure the user is input is a numeric value.
Reply:You're calling bank without any arguments. I hope you aren't planning on handing this in with "ur" and "u" in the output.
Reply:#include %26lt;iostream%26gt;


using namespace std;





/*if you put arguments in a function, it means you have to insert those arguments from 'the outside' of the function in order for the functions to work. In your case, you don't need arguments, since the inputted variables come from the inside of the function. */





double bank(){


double principle;


double year;


double rate;





cout%26lt;%26lt;"enter ur principle , please \n";


cin%26gt;%26gt;principle;


cout%26lt;%26lt;"enter how many years u put this principle , please \n";


cin%26gt;%26gt;year;





rate= (principle +year*0.06);


return rate;





}





int main(){


bank();


return 0;


}


Computer Programming C++?

Guys i need halp with a code this is the assigment:





Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.)





This is what i got thanks to dhvrm here in yahoo





// test 1p.cpp : Defines the entry point for the console application.


//





#include "stdafx.h"


#include %26lt;iostream%26gt;


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





using namespace std;





int _tmain(int argc, _TCHAR* argv[])


{





int input;


int i;


float x;


bool z = true;





cout %26lt;%26lt; "Input a positive integer:" %26lt;%26lt; endl;


cin %26gt;%26gt; input;


x = sqrt(input);





if(input % 2 != 0)


{


for(i = 3; i %26lt;= x; i + 2)


}


if(input % i == 0)


{


z = false;


exit;


}





}


else {


z = false;


}





if(z == true)


{


cout %26lt;%26lt; input %26lt;%26lt; " is a prime number!" %26lt;%26lt; endl;


}


return 0;


}








But it does not compile i really need help is for a project.

Computer Programming C++?
#include %26lt;math.h%26gt;


#include %26lt;iostream%26gt;





bool isPrime(int num) {


int i;





if (num %26lt; 2) return false;


else if (num % 2 == 0)


return num == 2;





for (i = 3; i %26lt; num; i += 2)


if (num % i == 0) return false;





return true;


}





int main(int argc, char **argv) {


int input;





std::cout %26lt;%26lt; "Input a positive Integer: ";


std::cin %26gt;%26gt; input;





if (isPrime(input))


std::cout %26lt;%26lt; input %26lt;%26lt; " is a prime number." %26lt;%26lt; std::endl;


else


std::cout %26lt;%26lt; input %26lt;%26lt; " is not a prime number. " %26lt;%26lt; std::endl;





return 0;


}
Reply:You need to understand what the lines of code are doing....adding in comments might help. Until you do there is no way you'll get this program working.





Here are some areas that need investigating





if(input % 2 != 0)


{


// i +2 does nothing here....you need to make it i = i +2 if you want i to be incremented by 2


for(i = 3; i %26lt;= x; i + 2)


// where is the statement for the for loop eg


//for (;;;)


//{


//printf("statement goes here");


//}


}








} // %26lt;-- this doesnt match up with anything....count them


else {


z = false;


}





3) if its not compiling for you rewrite the main function as follows, as you're not using the argc/argv parameters.





int main(void)


{


}








There's possibly more needed to be done to get it to compile and run and then you'll have to sort out any logic errors.....but hopefully that will help you get there.





Good luck.
Reply:exit;


should be


exit(1);
Reply:What line is the error?


Send me a email - powellinthesouth@hotmail.com


Correct my c ++ programe?

//sheet#2, Ex#2





#include%26lt;iostream%26gt;


using namespace std;





class HotelRoom // hotel room class definition


{





private:





int room_number ; //room number enter by user


int floor_number; //floor number enter by user


int type; //type of room enter by user


int booking; //booking enter by user





public:





SetRoomInfo(Ri); //


SetfloorInfo(Fi);


settypeoInfo(Ti);


setbookedInfo(Bi);








void ShowRoomInfo(){ // display show room info


cout%26lt;%26lt;"Number Of room is : "%26lt;%26lt; Ri;


cout%26lt;%26lt;"Number Of floor is : "%26lt;%26lt; Fi;


cout%26lt;%26lt;"type:" %26lt;%26lt;Ti;


cout%26lt;%26lt;booked:" %26lt;%26lt;Bi;





}





void CheckRoomStatus(){


If ( booking ==0 ) //if booking is equal to eual 0


booking free;





If ( booking ==1 ) //if booking us equal to equal 1


booking booked;





} // end if





void Booking(){


cout%26lt;%26lt;"what is advance of room";


cin%26gt;%26gt;the room number is 100,located in the second floor,it is a dubble room and available for booking





} // end function void chec room














}; // end class hotel room





//-----------------





int main() // function main begen brogram execution


{


hotel rom call;


call.setroom_number;


call.setfloor_nimber;


call.settype;


call.setbooking;





return 0;


}


//------------------- The End ----------------------

Correct my c ++ programe?
There's, like, a lot wrong with that. Recommend you get some serious, in-person help with your program.
Reply:I won't correct all of it, but I will put you more on track than you are now.





First, any class will have to have a constructor and a destructor even if they are only default versions.





Example:





class My_Class{


private:


int My_Num;


public:


void Set_My_Num();


void Show_My_Num();


My_Class(){} //Constructor


~My_Class(){} //Destructor


}; //Note the semi-colon, needed for class // definition.





Now, secondly, somewhere before you use the program, you need to define the functions for the class interface.


Examples:





void My_Class::Set_My_Num(){


int num;


cout %26lt;%26lt; "Enter an Integer: ";


cin %26gt;%26gt; num;


My_Num = num;


}





void My_Class::Show_My_Num(){


cout %26lt;%26lt; My_Num %26lt;%26lt; endl;


}





void main(){





My_Class object;


object.Set_My_Num();


object.Show_My_Num();


}





While the examples don't directly address the application you're writing, it should point out some omissions that are serious in your code.


I hope I've helped you out a bit. Pivy


Computer science C++ programing?

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


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





using namespace std;





int getstockprices;


float printaverage;


float printlowest;


float printhighest;


int main()


{


float stockprice [31];


int min=0;


int max=0;


float avg=0;


float total=0;





for(int day = 0; day %26lt; 31; day++)


{


void get_stock_price(float %26amp;stockprice);


cout%26lt;%26lt; "Please enter the closing stock price for day"%26lt;%26lt;day+1%26lt;%26lt;":"%26lt;%26lt;endl;


cin%26gt;%26gt;stockprice [day];





if (stockprice [day] %26gt; max)


max=day;


if (stockprice [day] %26lt; min)


min=day;


else if (day == 0)


min=day;


total+= stockprice[day];





}


cout%26lt;%26lt; "highest Price :"%26lt;%26lt;stockprice[max] %26lt;%26lt; " on day: " %26lt;%26lt; max%26lt;%26lt;endl;


cout%26lt;%26lt; "Lowest Price :"%26lt;%26lt;stockprice[min]%26lt;%26lt; "on day; "%26lt;%26lt; min %26lt;%26lt;endl;


cout%26lt;%26lt; "Average Price:"%26lt;%26lt;total/31.0%26lt;%26lt;endl;


system("PAUSE");


return 0;


}


tells 31 days of stock prices, highest the day it occurs, lowest the day it occurs and the average. it compiles but i don't know how to do this in functions. How can i write it in functions.

Computer science C++ programing?
first question though why do use globals all processing is done inside the main anyway and what's the use of the getstockprices function. i can't see its definition in there.





anyway, you can have a function for getting the input and storing this in the day array. if you don't know how to use pointers then declare day array as a global.





then have another function for processing the input and checking max and min values. you can declare this as global as well





then have another function for displaying the results





and that's it. i could write the code but that will be cheating
Reply:You are joking, right?

hydrangea

Computer science C++ programing?

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


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


using namespace std;


int getstockprices;


float printAverage;


float printLowest;


float printhighest;


int main()


{


int stockprice;


float average;


float lowest;


float highest;





void get_stock_price(float %26amp;stockprice)


cout%26lt;%26lt;"Please enter the closing stock price for day 1."%26lt;%26lt; endl;


cin%26gt;%26gt; stockprice;


int day;


for (day = 0; day %26lt; 31; day++ )





int main()





float stockprice;


get_stock_price(stockprice);


cout %26lt;%26lt; "1st price: " %26lt;%26lt; stockprice %26lt;%26lt; endl;





system("PAUSE");


return 0;


}


my program is not working i don't know what i am doing wrong..


i have to write a program with style and function to read 31days worth of stock prices from a user.after accepting 31days worth of stock prices the program will outpout.


the highest price and the day of the month that price was reached


the lowest price and the day of the month that price was reached


the average price for the month...


i did but when i run it only stops on day 1

Computer science C++ programing?
You didn't paste your code correctly. This is a bit of a mess and I know it's not the code you are compiling and running.





From the behavior you describe, I would say check your for loop. Make sure your brackets are correct and that you are waiting until you finish your for loop to pause and return.





for (day = 0; day %26lt; 31, day++)


{


float stockprice;


get_stock_price(stockprice);


//TODO*


}


//now print out your highest price, pause and return.





The TODO is that you have to write some code that will analyze which was the highest stock price so that you will be able to print the highest price after the for loop is through. It could be as simple as keeping track of whether each entered price is the highest one so far and saving it to a highestPrice variable if it is.
Reply:I'm surprised the code above compiles. It shouldn't unless I have completely forgotten how C does the precompile pass. It should have found several syntax errors. Either that, or you left out several bits of code.





Either way, you have the concept generally right, although some logic needs to change. For example, get_stock_price() should probably accept the day number to ask for. Running averages and actual averages differ, slightly so it may be best to create an array for all the values.





I would initialize lowest and highest to the first day's data and adjust during each iteration. I also don't see a reason to use a reference to return the stock price input by the user. Use the function's return value instead; that's what it's for.
Reply:google cprogramming site and read your code, don't be DAMN LAZY ***


How do I do a summation series with in c++ and have it print out the terms?

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


using namespace std;


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





int main()


{





std::cout %26lt;%26lt; "The Zeta Function" %26lt;%26lt; std::endl;


int a, n, m, x, sum, total;


std::cout %26lt;%26lt; "Enter value for m, where m is the number of terms in the series that the series begins on" %26lt;%26lt; std::endl;


cin %26gt;%26gt; m;


std::cout %26lt;%26lt; "Enter value for n, where n is the number of terms in the series that the series ends on" %26lt;%26lt; std::endl;


cin %26gt;%26gt; n;





for (int x = m; x %26lt;= n; x++)


total = (1/(x^2));





std::cout %26lt;%26lt; "The sum of the series is " %26lt;%26lt; total %26lt;%26lt; "\n";


return 0;


}





The math function doesn't work and I want every term sumed up to that point to be printed then I want the total sum to be printed. The function is the Zeta Function

How do I do a summation series with in c++ and have it print out the terms?
Your code is very close but there are a few things wrong with it.





1. ^ is a bitwise operator XOR which can actually result in zero which means your code will try an do an illegal division by zero if you set m and n to be 2 and 3 for example.





You need use pow here. 1/x^2 = x^-2 so you can use


pow (x , -2).





2. All variables have been declared as integers which means any division 1/y = 0 for all y%26gt;1. So for all x^2 %26gt; 1 the division will lose all remainder and be zero.





You need to declare your variables as doubles for maximum precision.





double a, n, m, x, sum, total;





3. You are overwriting the value total each loop iteration and total is never initialised.





So you need to keep adding to total once it has been initialised to zero.





total = 0;


for (int x = m; x %26lt;= n; x++)


total += pow (x , -2);





4. User could type in values so m %26gt; n which means the loop terminates immediately leaving total as zero so we can swap these values if that's the case.





if( m%26gt; n) {


a=m;


m=n;


n=a;


}





Here is a corrected version of your code:





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


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


using namespace std;





int main()


{


std::cout %26lt;%26lt; "The Zeta Function" %26lt;%26lt; std::endl;


double a, n, m, x, sum, total;


std::cout %26lt;%26lt; "Enter value for m, where m is the number of terms in the series that the series begins on" %26lt;%26lt; std::endl;


cin %26gt;%26gt; m;


std::cout %26lt;%26lt; "Enter value for n, where n is the number of terms in the series that the series ends on" %26lt;%26lt; std::endl;


cin %26gt;%26gt; n;


if( m%26gt; n) {


a=m;


m=n;


n=a;


}


total = 0;


for (int x = m; x %26lt;= n; x++)


total += pow (x , -2);





std::cout %26lt;%26lt; "The sum of the series is " %26lt;%26lt; total %26lt;%26lt; "\n";


return 0;


}





Additional checks you might want are seeing if the user has input non-zero values and actual integers.
Reply:nstead of doing x^2 which is not supported, do x*x
Reply:^ is a bitwise XOR .. not the pow





you can use pow





double pow(double x, double y);


long double powl(long double x, long double y);


double pow10(int p);


long double pow10l(int p);


double pow10(int p);


long double pow10l(int p);


Its a c++ program, plz helpppp?

#include %26lt;conio.h%26gt;// needed for getch


#include %26lt;iostream.h%26gt;//needed for cout message


#include %26lt;math.h%26gt;// need got math


#include %26lt;iomanip.h%26gt;//


#include %26lt;string.h%26gt;// needes for strings


void startup();


void method(double sqrt(double num));


int main()


{


startup();


method(double sqrt(double num));


getch();


return 0;


}


void startup()


{


cout%26lt;%26lt;""%26lt;%26lt;'\n';


cout%26lt;%26lt;"Jan 24, 2008"%26lt;%26lt;'\n';


cout%26lt;%26lt;"pg162 problem 9.1.2"%26lt;%26lt;'\n';


cout%26lt;%26lt;""%26lt;%26lt;'\n';


}


void method(double sqrt(double num))


{


int num;


cout%26lt;%26lt;"Enter a number to be squared rooted: ";


cin%26gt;%26gt;num;


cout%26lt;%26lt;num;


}








i'm trying to find the sqrt of the number the user is going to enter but my book just says that the function is sqrt. plz help. thx

Its a c++ program, plz helpppp?
http://www.cplusplus.com/reference/clibr...





In function method fuggetyerparameter. declare int num as double num and replace the line "cout%26lt;%26lt;num;" with "cout%26lt;%26lt;sqrt(num);"





Programs that use conio.h tend to be c, not c++ because it is a c library. For getch(); you might try "system("pause");". System is in the stdlib.h header, which I believe is #included in a couple of the headers you are using.





Now can I go back to sleep?


Write a c++ program that prompt the user to enter a number and then display a series of numbers as follows:?

for instance, if the user enters number 4 ..the program should display:





1


22


333


4444





* I did solve this Q:





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


void main() {


int n;


cin%26gt;%26gt;n;


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


{


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


cout%26lt;%26lt;i;


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


}


}


}





But when i run it on the compiler ... it gave this output





1111


222


33


4





Can someone help plz..

Write a c++ program that prompt the user to enter a number and then display a series of numbers as follows:?
Change


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


to


for(int j=1;j%26lt;=i;j++)
Reply:#include %26lt;stdio.h%26gt;


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


void main()


{


int i,j,k,x;


clrscr();


printf(" Enter A Num : ");


scanf("%d",%26amp;x);


k=1;


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


{


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


printf("%d",k);


printf("\n");


k++;


}


getch();


} Report It

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


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


void main()


{


int n;


cout%26lt;%26lt;"enter the number\n";


cin%26gt;%26gt;n;


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


{


for(int j=1;j%26lt;=i;j++)


cout%26lt;%26lt;i;


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


}


getch();


}
Reply:... wtf is that?

hawthorn

Whats wrong with this c++ program?

I need to output the sum of the square of the odd numbers between firstnum and secnum. I also need to output the sum of all the even numbers between firstnum and secnum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;


using namespace std;


int main()


{


int firstnum,secnum, i, total=0, a, b;


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


cin%26gt;%26gt;firstnum;


cout%26lt;%26lt;"Enter a second integer (must be less then the first integer enterd):";


cin%26gt;%26gt;secnum;


cout%26lt;%26lt;"your enterd :"%26lt;%26lt;secnum%26lt;%26lt;endl;


if(firstnum%26lt;secnum)


cout%26lt;%26lt;endl;


else


if (firstnum%26gt;secnum)


cout%26lt;%26lt;"Please re-run program and enter numbers again"%26lt;%26lt;endl;


for (i=firstnum; i%26lt;= secnum; i++)


if(i % 2 != 0)


cout %26lt;%26lt; i %26lt;%26lt; " is an odd number between " %26lt;%26lt; firstnum %26lt;%26lt; " and " %26lt;%26lt; secnum %26lt;%26lt; endl;


cout%26lt;%26lt;"The sqrt is"%26lt;%26lt;sqrt(i)%26lt;%26lt;endl;





for (i=firstnum; i%26lt;= secnum; i++)


if(i % 2 == 0)


cout %26lt;%26lt; i %26lt;%26lt; " is an even number between " %26lt;%26lt; firstnum %26lt;%26lt; " and " %26lt;%26lt; secnum %26lt;%26lt; endl;


total += i;


cout %26lt;%26lt; "The total of all even numbers from " %26lt;%26lt; firstnum %26lt;%26lt; " to " %26lt;%26lt; secnum %26lt;%26lt; " is " %26lt;%26lt; total %26lt;%26lt; endl;


cout%26lt;%26lt;"\nPart B"%26lt;%26lt;endl;


a=1;


while(a%26lt;=10)


{


cout%26lt;%26lt;a%26lt;%26lt;" the sqrt "%26lt;%26lt;sqrt(a)%26lt;%26lt;endl;


a=a+1;


}


system("pause");


return 0;

Whats wrong with this c++ program?
There are numerous problems, but I believe these are the three most important ones:





I. It doesn't follow the specification


As written, your program looks like it was supposed to output the following:


1. The odd numbers between firstnum and secnum.


2. The square roots of those numbers.


3. The even numbers between firstnum and secnum.


4. The sum of all the even numbers between firstnum and secnum


5. The square roots of the whole numbers 1 through 10.


Of those 5 things, only one (number 4) is among the things you originally said the program is supposed to output.





II. For loops need correction


If you want several statements to be executed on each iteration of a for loop, you have to put those statements inside curly braces, like this:


for (i = firstnum; i %26lt;= secnum; i++)


{


(the statements go here)


}


Otherwise, only the first statement after the for statement is executed on each iteration of the loop.





III. Numbers aren't squared correctly


If you want the square, as opposed to the square root, of a number a, then you need to use a**2 or a*a, not sqrt(a).


Ok... i wrote it... now need help completing it (c++)?

i need to times the amount of adult tickets with $6.00 and the amount of child tickets with $3.00. Then i need to add those two together. where do i need to add the info? and what is that info%26gt;?


#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main(int argc, char *argv[])


{


cout %26lt;%26lt; "This project was written by Jacob A. DePratti" %26lt;%26lt; endl;





char moviename[50], adult[50], child[50];


int Six,Three,Total;





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





cout %26lt;%26lt; "Enter the movie's Name: ";


cin.get(moviename,50);


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


cout %26lt;%26lt; "Enter the number of adult tickets sold: ";


cin %26gt;%26gt; adult;


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


cout %26lt;%26lt; "Enter the number of child tickets sold: ";


cin %26gt;%26gt; child;


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





cout %26lt;%26lt; "***************************************... %26lt;%26lt; endl;


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


cout %26lt;%26lt; "INCOME STATEMENT" %26lt;%26lt; endl;


cout %26lt;%26lt; "******************" %26lt;%26lt; endl;





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


cout %26lt;%26lt; "Movie Name: "%26lt;%26lt;moviename%26lt;%26lt; endl;


cout %26lt;%26lt; "Adult Tickets Sold: "%26lt;%26lt;adult %26lt;%26lt; endl;


cout %26lt;%26lt; "Child Tickets Sold: "%26lt;%26lt;child%26lt;%26lt; endl;


cout %26lt;%26lt; "Gross Box Office Profits: " %26lt;%26lt;endl;


cout %26lt;%26lt; "Net Box Office Profit: " %26lt;%26lt; endl;


cout %26lt;%26lt; "Amount Paid To Distributers" %26lt;%26lt; endl;


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


























system("PAUSE");


return EXIT_SUCCESS;


}

Ok... i wrote it... now need help completing it (c++)?
add anywhere after the cin





int childDollars = child * 3;


int adultDollars = adult *6;








then cout them as you did the rest.
Reply:programming is done well but wat bout mathematics.


you made no. of tickets variable as adult and child but they are character variables made them integer





then calculate total


use arithmetic to solve that
Reply:Gee, haven't done some C++ programming for a long while (like, maybe four years). But I do get the logic of your program, all you need to do is create three additional variables for the data that you need %26amp; create a formula.





Let's call them AdultSales, ChildSales %26amp; GrossSales. You declare them along with your variables for the tickets, like this:





int Six,Three,Total,AdultSales,ChildSales,Gr...








Then you create the formula:





AdultSales = adult*6;


ChildSales = child*6;


GrossSales = AdultSales + ChildSales;





Be sure to insert this formula expression before your code for screen output.








Then you modify you code for your output:





. . . . .


cout %26lt;%26lt; "Adult Tickets Sold: "%26lt;%26lt;adult %26lt;%26lt; endl;


cout %26lt;%26lt; "Child Tickets Sold: "%26lt;%26lt;child%26lt;%26lt; endl;


cout %26lt;%26lt; "Gross Box Office Profits: "%26lt;%26lt;GrossSales %26lt;%26lt;endl;


. . . . .








Hope that solves your problem.


Need help regarding DEV C++?

The problem is like this: I write the code, it works cause the compiling is made right to the end and the software provides me a bonded executable, the thing is the application is a dos one, and when I run it the dos window doesn't stick around long enough to see the result. I have learned I can force it to stick aroun with the 'cin.get();" after the main function, but don't have an example of usage of this cin.get(). Can you give me a working example of this thing that I am talking about.?

Need help regarding DEV C++?
Try running your programs from a DOS prompt instead of from Windows. That way it's running in a window that's already open, and will remain open after program termination. You'll be able to see your output just fine, without having to press a key to finish your program every time.





...or code on Linux ;)





Rawlyn.
Reply:are you using bloodshed dev c++? maybe you forgot a semicolon or a bracket or something. can you paste your code in details so maybe i can help you debug.





sure email it to me. i enabled it just now :)


Why my C++ program is not working?

i am doing functions and when i build my solution i get a 0. why? this is my code:


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::fixed;





//funtions prototypes


double getTestScore(double, double, double);


double calcAverage(double);





int main()


{


//declare variables


double firstScore = 0.0;


double secondScore = 0.0;


double thirdScore = 0.0;


double average = 0.0;


double score = 0.0;





//function calls


score = getTestScore(firstScore, secondScore, thirdScore);


average = calcAverage(score);





//display information





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt; "Your average is: " %26lt;%26lt; average %26lt;%26lt; endl;


return 0;


}





//*****************************PROGRAM DEFINED FUCTION********************************


double getTestScore(double firstScore, double secondScore, double thirdScore)


{

Why my C++ program is not working?
After reviewing your getTestScore function this is what i am seeing...When you are returning the double totalScore, your are returning a value of 0.0





double totalScore = 0.0;


input a


input b


input c


return totalScore;





I do not see you using the actual input that your getting. So before you return the value, make sure you set totalScore before passing it back like so:





double totalScore = 0.0;


input a


input b


input c





totalScore = input a + input b + input c;


return totalScore;
Reply:In the function getTestScore, the totalScore variable is assigned a value of 0. It is not modified in any way in the function, you need to add the three inputs together and assign that value to total score inside the function before you return totalScore. As in:





totalScore = (Score1 + Score2 + Score3...etc);





Then your function will return a value other than 0.





oh, and for the top, you can just use using namespace std; instead of all the std:: stuff.
Reply:The 3 parameters to getTestScore are passed by value, not by reference. They don't update back in main() when you do an assignment in the subroutine.





Try passing them by reference, or restructuring in some other way.
Reply:Your function getTestScore always returns 0.0. You initialize totalScore to 0.0, never change it, and return totalScore.





Also, the arguments firstScore, secondScore, and thirdScore are serving no purpose, you don't need them. I would say the function should be called getTotalScore. The only thing you're missing is to set totalScore to the sum of the entered scores.

marguerite

Why my C++ program in not working?help please!!?

i get an error that says: Error C2065: 'test1' : undeclared


test1 has been declare - what is the problem? this is my code:


#include%26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std:: cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::fixed;





//fucntion prototype


void getTestScores (double%26amp;, double%26amp;, double%26amp;);


double calcAverage(double%26amp;, double%26amp;, double%26amp;);


void displayAverage(double);





int main ()


{


//decalare variables


double testl = 0.0;


double test2 = 0.0;


double test3 = 0.0;


double average = 0.0;


double getAverage = 0.0;





//enter functions to het input items


getTestScores(testl, test2, test3);


getAverage = calcAverage(test1, test2, test3);


displayAverage(average);





return 0;


} //end of main function





//**program defined functions ****

Why my C++ program in not working?help please!!?
Looking at the code you have 2 different variables - one called 'test1' with a number one, and you also have one called 'textl' with a lower case 'L' -- this one happens to be the one that is declared.
Reply:www.kosamba.com


Pay these indians to do your programming... dont pay over 20 bucks!
Reply:In the code you pasted, it looks like you have some that are "test L" and some that are "test1". Verify that all of them are correct.


Whats wrong with this c++ program?

the answerr that always comes out is one.





class alvarez{


protected:


double Inter;


double time;


double percentage;


public:


void CalcArea();


void ShowArea();


alvarez(double I=0, double t=0);


};


alvarez::alvarez(double I,double t)


{


I=Inter;


t=time;


}


void alvarez::CalcArea()


{


percentage= Inter/time;


}


void alvarez::ShowArea()


{


cout %26lt;%26lt;"the percentage is:"%26lt;%26lt; percentage %26lt;%26lt;endl;


}





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


#include "alvarez.h"





class ThreeD: public alvarez


{


protected:





double Percentage;





public:


void CalcVol();


void ShowVol();


ThreeD (double I=0, double t=0);


};





ThreeD :: ThreeD(double I, double t):alvarez(I,t)


{





}


void ThreeD::CalcVol(){


Percentage=Inter/time;


}


void ThreeD::ShowVol(){


cout %26lt;%26lt; "The percentage is: " %26lt;%26lt; Percentage %26lt;%26lt; endl;


}





main(){


double I,t;


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


cin %26gt;%26gt; I;


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


cin %26gt;%26gt; t;


ThreeD box (I,t);


box.CalcVol();


box.ShowVol();


return(0);


}

Whats wrong with this c++ program?
The result from the above-stated codes will always be 1 because zero divided by zero is ONE. (0 / 0 = 1).





Take a better look at your constructor that takes in two arguments 'l' and 't'. It should be the integer variables 'Inter' and 'time' that receives the value of the arguments l and t and not vice versa...





This is how to solve it...





alvarez::alvarez(double l, double t)


{


Inter = I;


time = t;


}

tropical flowers