Monday, May 24, 2010

Very Basic C++ Question About Float!?

I've just started a C++ class and we have to write a program that asks for 5 grades from the user and then calculates the average. I almost have the whole thing down and it compiles and runs smoothly, but then my teacher reminded us that the answers could turn out to be decimals, of course. I know that float is used for decimals but we haven't done any examples with this type of variable. I'm guessing that I'll just have to do something in this section:





#include%26lt;iostream%26gt;


using namespace std;





int main ()


{


string username;


int grade1;


int grade2;


int grade3;


int grade4;


int grade5;


int avg = 0;


int sum = 0;





Thanks in advance!

Very Basic C++ Question About Float!?
in above programs or hints do a s\mall change that is


avg=float(sum)/5.0;


or


avg=float(grade1+grade2+grade3+grade4+...
Reply:not sure about c++, but try to do this





float avg = 0.0;





sum = grade1 + grade2 + grade3 + grade4 + grade5 ;


avg = sum / 5.0 ;





note the last line is over 5.0 not over 5, since i think over 5 will just round the actual average before assigning it to avg
Reply:Ok. floats are just like ints but can do decimals. So just change your average variable into a float like this:





float avg = 0;





That's it!
Reply:A float is used to store a value corresponding to a single-precision real number ( a decimal number ). Since you are trying to calculate the average, you have to change the line:





int avg = 0;





to





float avg = 0.0;





Then you can calculate the average using:





avg = (grade1 + grade2 + grade3 + grade4 + grade5)/(5.0);





or, if you prefer: avg = sum/(5.0);





I hope this helps you.


C++ string question?

OK! Just to let everyone know...this project has already been turned in--I just need to know what I could've done to make the program take str2 as a string of characters and erase all of them. I could not get the for loop to keep searching/ finding anything past the first occurrence. I need to know for a coming final in c++ in 2 weeks. thanks





#include%26lt;iostream%26gt;





#include%26lt;string%26gt;





using namespace std;





int main()





{





string str1, str2, ans;





do{





cout%26lt;%26lt;"Please enter a string (40 characters max)--%26gt;"%26lt;%26lt;endl;


getline(cin, str1);





cout%26lt;%26lt;"Please enter the characters to be removed(20 characters max) --%26gt;"%26lt;%26lt;endl;


getline(cin, str2);





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


{





string::size_type loc=str1.find(str2, 0);


(loc!=string::npos);


{


str1.erase(loc, +1);





cout%26lt;%26lt;"First string with characters removed --%26gt;"%26lt;%26lt;str1%26lt;%26lt;endl;





}


}








cout%26lt;%26lt;"Would you like to do another(y or n)?"%26lt;%26lt;endl;


getline(cin, ans);


}


while(ans[0]=='y'||ans[0]=='Y');


return 0;


}

C++ string question?
One reason your 'for' loop doesn't work is the semi-colon at the end of it:


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





Another problem is this statement, that looks like you wanted it to be an 'if' statement:


(loc!=string::npos);





Your compiler should have told you this statement has no effect.





Even with that, there are problems with the code. There is no need to hardcode 40 in your for loop, or prompt for a max of 40 or 20 characters. Also, with this: str1.erase(loc, +1) you'll only erase str2 if its length is 1. Also, to be more efficient, you don't need to repeatedly start your search at str1[0].





So, implementing all those suggestions, my improved version of your code looks like this:





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


string str1, str2, ans;


string::size_type loc = 0;





do {


cout%26lt;%26lt;"Please enter a string --%26gt; ";


getline(cin, str1);


cout%26lt;%26lt;"Please enter the characters to be removed --%26gt; ";


getline(cin, str2);


do {


if ((loc = str1.find(str2, loc)) != string::npos) {


str1.erase(loc, str2.length());


cout %26lt;%26lt; "First string with characters removed --%26gt;" %26lt;%26lt; str1 %26lt;%26lt; endl;


}


} while (loc != string::npos);


cout %26lt;%26lt; "Would you like to do another(y or n)?";


getline(cin, ans);


} while (ans[0]=='y' || ans[0]=='Y');


return 0;


}





One other thing I might do for a production version of the program would be to move the 'cout' outside of the 'for' loop, so you just display the final result. As it is, though, it's nice to watch the progress as occurrences of str2 are removed.





EDIT: it just occurred to me that str2 probably isn't a substring to be removed, but a list of characters to be removed. If so, you were missing something for this as well. Check out the code below. I think this is the solution you're looking for:





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


string str1, str2, ans;


string::size_type loc = 0;





do {


cout%26lt;%26lt;"Please enter a string --%26gt; ";


getline(cin, str1);


cout%26lt;%26lt;"Please enter the characters to be removed --%26gt; ";


getline(cin, str2);


if ((str1.length() %26gt; 0) %26amp;%26amp; (str2.length() %26gt; 0)) {


do {


while ((loc = str1.find(str2[0], 0)) != string::npos) {


str1.erase(loc, 1);


}


str2.erase(0,1);


} while (str2.length() %26gt; 0);


cout %26lt;%26lt; "First string with characters removed --%26gt;" %26lt;%26lt; str1 %26lt;%26lt; endl;


}


cout %26lt;%26lt; "Would you like to do another(y or n)?";


getline(cin, ans);


} while (ans[0]=='y' || ans[0]=='Y');


return 0;


}

geranium

C++ run-time failure. please help?

I got this message using visual studi c++:


Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted.





here is my function:


void highscore(int score)


{


int indexes=0,temp[5];


ifstream infile;


infile.open("filesout.txt");


if (infile.is_open())


{


while (!infile.eof())


{


infile%26gt;%26gt;temp[indexes++];


}


}


else


cerr %26lt;%26lt;"error occured when opening file";


infile. close();





int a=1,k=0,s;


temp[5]=score;


while (k%26lt;6)


{


for (int m=a;m%26lt;6;m++)


{


if (temp[m]%26gt;temp[k])


{


s=temp[k];


temp[k]=temp[m];


temp[m]=s;


}


}


k=k+1;


a=a+1;


}


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


cout %26lt;%26lt;temp[y]%26lt;%26lt; endl;





ofstream outfile;


outfile.open("filesout.txt");


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


outfile %26lt;%26lt;temp[x]%26lt;%26lt;endl;


outfile.close();


}





what the function do is put the input extracted from a file "filesout.txt" and reorder from highest to lowest


can anyone check the error please. thank you

C++ run-time failure. please help?
One obvious error is the dimension on temp and the loop variables. Note how your dimension on temp is 5; however, you specifically reference temp[5] in your "for(int m=a" loop. All references into your temp array should only be 0 to 4 with a dimension of 5.





i.e. any reference to "var%26lt;6" then "temp[var]" will throw an error.


C++ Leap Year Program with error C2659: '=' : function as left operand problem?

Okay here is my program, it has only two compiles errors: C2659: '=' : function as left operand. I'm new to C++ and get confused by this error, can anyone correct my program? Thanks. Here is the program:





#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;





using namespace std;





int getYear();


bool isLeap(int year);


char moreData();





int main()


{


int year;


char carryOn='y';





while(carryOn=='y'||carryOn=='Y'){


year=getYear();





if(isLeap(year)){


cout%26lt;%26lt;" is a leap year\n";


}else{


cout%26lt;%26lt;" is not a leap year\n";


}


carryOn=moreData();


}


system("pause");


}


int getYear(){


int year;


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


cin%26gt;%26gt;year;


return year;


}


bool isLeap(int year){


if(year%4==0){


if (year%100!=0){


isLeap=true;


}else if (year%400==0){


isLeap=true;


}


//return isLeap;


}}


char moreData(){


char carryOn;


cout%26lt;%26lt;"Do you want to enter more data? (y/n) ";


return carryOn;


}

C++ Leap Year Program with error C2659: '=' : function as left operand problem?
in your isLeap() function.. you are assigning isLeap=true.... which cannot be done.. because isLeap is the pointer to the function isLeap().. dont worry if you dont understand that..... But you can use the statement


return (true);


instead of


isLeap=true;


and use the statement


return(false);


at the end of the isLeap function just before the closing flower brace.. that will make the program work...
Reply:I think your





isLeap=true;





statements need to be





return true;





instead!


Reading text files in C++?

How can you read text and integers from a file in C++ and store them as strings and varibles for use in the program. The text file formated like string, string, string, int, int, int, int over serveral lines like below.





Word1 Word2 Word3 Int Int Int


Word1 Word2 Word3 Int Int Int





and so on...

Reading text files in C++?
fscanf()? http://www.cplusplus.com/reference/clibr...


HELP ME (C++!!), PROGRAM INSIDE!!?

#include%26lt;iostream%26gt;


int main()


using namespace std;


{


int x=12;


cout%26lt;%26lt;hex%26lt;%26lt;x;


return 0;


}


__________________________________


The result is "C", but i need it to display "00000C", i thought of doing it with IF statements and other variables, but that would slow down the program...

HELP ME (C++!!), PROGRAM INSIDE!!?
cout %26lt;%26lt; setfill ('0') %26lt;%26lt; setw (6);


cout %26lt;%26lt; hex %26lt;%26lt; 12 %26lt;%26lt; endl;





I'm not an expert on cout

sweet pea

Can someone please help me with some C++ (programming) questions?

24. In testing code having an if or an if-else clause, describe how to construct test data. Give an example based on:





if (x %26gt; y)


statement1;


else


if (u %26gt; v)


statement2;


else


statement3;





25. Answer the question at the end of this code.





const int x = 1.23456789;


int f(int y)


{


int z;


//body of function: int f(int)


}


const int w = 9.987654321;


int g(double u)


{


int v;


// body of function: int g(double)


}


int main ()


{


int m;


double nn;


// body of function main()


m = g(nn);


// ...


m = f(x);


return 0;


}





In which functions can each variable be accessed?





a)x e) w


b)f f) g


c)y h) v


d)z i) m


j)nn





26. Why not make all variables non-constant global variables, and avoid the bother of parameters for functions?





27. Describe the similarity and difference between a function's parameters and function's local variables.

Can someone please help me with some C++ (programming) questions?
Do we get to share your class grade too? You at least should try to answerthem and then ask if your answers make sense.