Tuesday, July 14, 2009

Need help with this?

Write a C++ program that opens a text file with the name "Hello.txt", creates a second file "Hello1.txt". Read the content of Hello.txt and copy its contents to Hello1.txt " replacing every space character ' ' with a '_' character. Thus the text "The quick brown fox jumps over the lazy dog." in Hello.txt should become "The_quick_brown_fox_jumps_over_the_lazy... Close both files. Remember to include string and fstream. You will have to use the get function of your ifstream object.

Need help with this?
You basically want us to do your homework for you? That's not gonna happen. You can't just paste your assignment and hope for us to write the code for you.





Tell us what exactly you need help with and we'll help you out.





By the way: This is basic C++ streams. If you can't get this to work then you should stop studying IT.
Reply:Monser cons wet files un zip files Monser_cons_wet_ unzipped%26lt;(223551.5881.33.)%26gt;3l!73%26gt; Intraction notice **lector text Img.1 messer face Stream *elavation 117Q Detracts messer face.Now intract notice invert 117 into the 5881.


Can someone convert the code to java...... !!!?

////////////////////////////////////////...


// ShapeFileReader.cpp: implementation of the ShapeFileReader class.


//////////////////////////////////////...





#include "iostream.h"


#include "fstream.h"





#include "ShapeFileReader.h"


#include "MainHeader.h"


#include "RecordHeader.h"


#include "Point.h"


#include "Polygon.h"


#include "MultiPoint.h"


#include "PolyLine.h"





const char* FILENAME = "c:/shapes/cities.shp";


int getTotalShapes();





MainHeader mh;


Point *pt;


MultiPoint *mp;


Polygon *pg;


PolyLine *pl;





void main()


{


RecordHeader rh;


ifstream inp;


int index=0;





int l_totalShapes = getTotalShapes();





inp.open(FILENAME,ios::in, filebuf::openprot);


inp.setmode(filebuf::binary);


inp.read((char*)%26amp;mh, 100);





cout %26lt;%26lt; "\n File Code = " %26lt;%26lt; mh.getFileCode();


cout %26lt;%26lt; "\n File Length = " %26lt;%26lt; mh.getFileLength();


cout %26lt;%26lt; "\n Version = " %26lt;%26lt; mh.getVersion();


cout %26lt;%26lt; "\n Shape Type = " %26lt;%26lt; mh.getShapeType();


cout %26lt;%26lt; "\n xMin = " %26lt;%26lt; mh.getXMin();


cout %26lt;%26lt; "\n yMin = " %26lt;%26lt; mh.getYMin();


cout %26lt;%26lt; "\n xMax = " %26lt;%26lt; mh.getXMax();


cout %26lt;%26lt; "\n yMax = " %26lt;%26lt; mh.getYMax();


cout %26lt;%26lt; "\n zMin = " %26lt;%26lt; mh.getZMin();


cout %26lt;%26lt; "\n zMax = " %26lt;%26lt; mh.getZMax();


cout %26lt;%26lt; "\n mMin = " %26lt;%26lt; mh.getMMin();


cout %26lt;%26lt; "\n mMax = " %26lt;%26lt; mh.getMMax();





cout %26lt;%26lt; "\n Total shapes = " %26lt;%26lt; l_totalShapes;





switch (mh.getShapeType())


{


case POINT:


pt = new Point[l_totalShapes];


break;


case POLYLINE:


pl = new PolyLine[l_totalShapes];


break;


case POLYGON:


pg = new Polygon[l_totalShapes];


break;


case MULTIPOINT:


mp = new MultiPoint[l_totalShapes];


break;


}





while (index %26lt; l_totalShapes)


{


inp.read((char*)%26amp;rh,8);


if(inp.eof())


break;








switch(mh.getShapeType())


{


case POINT:


inp.read((char*)(pt+index),20);


break;





case POLYLINE:


{


int *l_ptrParts = NULL;


struct point *l_ptrPoints = NULL;





inp.read((char*)(pl+index),44);





l_ptrParts = new int[pl[index].getNumParts()];


l_ptrPoints = new struct point[pl[index].getNumPoints()];


pl[index].setParts(l_ptrParts);


pl[index].setPoints(l_ptrPoints);





inp.read((char*)l_ptrParts, (sizeof(int)*pl[index].getNumParts()));


inp.read((char*)l_ptrPoints, (sizeof(struct point)*pl[index].getNumPoints()));





}


break;





case POLYGON:


{


int *l_ptrParts = NULL;


struct point *l_ptrPoints = NULL;





inp.read((char*)(pg+index),44);





l_ptrParts = new int[pg[index].getNumParts()];


l_ptrPoints = new struct point[pg[index].getNumPoints()];


pg[index].setParts(l_ptrParts);


pg[index].setPoints(l_ptrPoints);





inp.read((char*)l_ptrParts, (sizeof(int)*pg[index].getNumParts()));


inp.read((char*)l_ptrPoints, (sizeof(struct point)*pg[index].getNumPoints()));





}


break;





case MULTIPOINT:


{


struct point *l_ptrPoints = NULL;





inp.read((char*)(mp+index),40);





l_ptrPoints = new struct point[mp[index].getNumPoints()];


mp[index].setPoints(l_ptrPoints);





inp.read((char*)l_ptrPoints, (sizeof(struct point)*mp[index].getNumPoints()));


}


break;





default:


cout %26lt;%26lt; "\n The shape type " %26lt;%26lt; mh.getShapeType () %26lt;%26lt; " is not handled yet\n";


}





// Increment index to point to next shape object in the shape array


index ++;


}





inp.close();


}





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


* getTotalShapes


* @return int Returns the total number of shapes in the shape file


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





int getTotalShapes()


{


ifstream inp;


RecordHeader rh;





int l_shapeCount = 0;





inp.open(FILENAME, ios::in, filebuf::openprot);


inp.setmode(filebuf::binary);





inp.seekg(100, ios::beg);





while(!inp.eof())


{


inp.read((char*)%26amp;rh,8);


if(inp.eof())


break;


inp.seekg(rh.getContentLength()*2, ios::cur);


l_shapeCount++;


}





inp.close();





return l_shapeCount;


}

Can someone convert the code to java...... !!!?
If you are still stuck, may be you can contact a java expert at websites like http://askexpert.info/ to help you finish your assignment.
Reply:go to rentacoder.com


pay some money and someone could do it for u


i think it will cost u about 10$


Help me with this program!! code inside, for making multiple filenames acording to an int value!?

#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;fstream%26gt;


using namespace std;


int main ()


{


int a,b=0;


string fileN="";


ofstream record;


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


cin %26gt;%26gt; a;


while (b %26lt; a)


{


fileN=b + ".txt";


b++;


record.open (fileN.c_str());


record %26lt;%26lt; "probandolo"%26lt;%26lt;b;


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


record.close();


}


return (0);}

Help me with this program!! code inside, for making multiple filenames acording to an int value!?
You can't just add strings together with the '+' operator. You need to create or use a function which can stitch 2 strings together.


There is a C function called strcat() which will do the job. Also, you need an ASCII representation of a decimal number. Use a function like itoa() to convert an integer into a string representation of a number.





Example:


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


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


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


char emptystring[16];


char name[]="Myfile";


char numeral[5];


int number=40;


char ext[]=".txt";





int main(void)


{


strcpy(emptystring, name); // copy 'name'


itoa(number, numeral, 10); // convert to str


strcat(emptystring, numeral); // append str


strcat(emptystring, ext); // append str


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


return 0;


}





strcat() and strcpy() requires string.h, and itoa() requires the stdlib.h header file. Look in your compiler's help file for more information on these functions.


Data handling( very basic program)?

i want to write a program to display the contents of file using get() function. i did not get output in Turbo C++. why???


please help me





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





int main()


{


char ch;


ifstream fin;


fin.oprn("marks.dat",ios::in);


if(!fin)


{


cout%26lt;%26lt;"\n can't open file";


return 1; // what does this statement do????


}


while(fin)


{


fin.get(ch);


cout%26lt;%26lt;ch;


}


fin.close();


return 0;


}

Data handling( very basic program)?
Not sure if this is just a typo, but you have shown here:





fin.oprn("marks.dat",ios::in);





that should be open, with an 'e'





But since you're not getting a compile error, I'm guessing that's not the problem.





while(fin) will always return true, therefore it will never exit the loop.





try this while loop instead:





while(fin.get(ch))


{


cout %26lt;%26lt; ch;


}





And you ask in your code what return 1; does..





main() is a function, and since you put "int main()" you're saying this function will return an integer. When you put return 1, you're saying "I've found the answer, it is 1, so we can exit the function now with the answer of 1"





In other words, it exits the function at that point. If you had some other function calling main() from somewhere else, you could use that data. But since you aren't, it doesn't really matter what number you return.





Make sense?

flower garden

Problem in reading floating point numbers using Dev-cpp?

I'm having problem in reading some floating point numbers in C. When I use fstream and read say 4.56 - it stores it as 4.5599999999999.





I know from theory that 4.5599999... and 4.56 are equal but I need 4.56 not the one with infinite 9s.





So please help me. Is there a way I can read it as 4.56 or if not is there a function I can use to convert 4.5599999 to 4.56?

Problem in reading floating point numbers using Dev-cpp?
You can't help the input conversion -- decimal fractions don't always (all right, rarely) convert exactly to binary fractions, and the input conversion has to live with that. But you can control the output so that it looks tidier. Study the standard C output format conversions, the f conversion in particular, and limit the number of decimal places. %.2f in this case. That should produce the rounded result you're looking for.





Good luck.
Reply:My gut instinct is that it is a problem with your compiler. You may want to get a different compiler:





http://openlist.asponge.com/getInfo.php?...
Reply:to get clean output, you need to use the formatting specifications in your printf statement. For two digits after the decimal point, use %.2f





There is never a guarantee that internally floating point numbers will match exactly, even if math says that they should. Many times when a computer adds, subtracts, multiplies, or divides floating point numbers a little bit of error will creep in.


No matter how many bits it uses, the representation is always an approximation. In decimal notation, it takes an infinite number of digits to write the value 1/3. 0.3333 is an approximation. 0.333333333333333333 is better, but still an approximation. Since a floating point number is stored in binary, 0.2 is infinitely repeating like 1/3 in the decimal system.


Integers do not have this problem. You might try using an internal representation that multiplies everything by 100. then divide it just before you print it.


Hi, I'm an intermediate programmer, can you give me some graphical code for c++??

please, I need some code that will work with visual studios 2008 in that it doesn't have:


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


and if that program uses graphic.h, please tell me where to download graphic.h.


I want some easy sorce code,


Thank You very much

Hi, I'm an intermediate programmer, can you give me some graphical code for c++??
i have some codes which i posted right in this link... http://pscode.com/vb/scripts/ShowCode.as... ..or visit my blog and just leave a comment at http://ronaldborla.blogsome.com/ or http://ronaldborla.wordpress.com/


Also http://ronaldborla.blogsome.com/2008/02/... for my biggest announcement in my blog... (^^,) Thanks!


Plz help reg FILE I/O in c++?

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





void read(ifstream %26amp;T) //pass the file stream to the function


{





char ch;





while(!T.eof())


{


T.get(ch);


cout %26lt;%26lt; ch;


}





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


}





void main()


{


ifstream T("file1.txt");


read(T);


T.close();





T.open("file2.txt");


read(T);


T.close();


}


HERE FILE1 And FILE2 ARE TWO TXET FILES.FILE 1 IS GETTING OPENED BUT FILE2 IS NOT.


WATS THE PROBLEM??

Plz help reg FILE I/O in c++?
try this for an example, you will have to modify the code, I won't do your homework for you, but this should help you out








#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





bool Load(ifstream%26amp;);


bool ReadData(ifstream%26amp;, char[],int%26amp;);


void PrintData(const char[],int);


void clear(ifstream%26amp;);








void sort(char[],int);


void swapValues(char%26amp;,char%26amp;);


int indexOfSmallest(const char a[], int startIndex, int numberUsed);








int main()


{


ifstream file;


int iSize;


char cData[100];





try


{


if(!Load(file))


throw "File Handle could not be acquired";





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


{


if(!ReadData(file,cData,iSize))


throw "Data could not be read";


cout %26lt;%26lt; iSize %26lt;%26lt; endl;


PrintData(cData,iSize);


sort(cData,iSize);


PrintData(cData,iSize);


}





file.close();


}


catch(char* szError)


{


cout %26lt;%26lt; endl %26lt;%26lt; "Error: " %26lt;%26lt; szError %26lt;%26lt; endl;


exit(0x01);


}





return 0;


}





bool Load(ifstream%26amp; iFS)


{


try


{


iFS.open("c:\test.txt",ios::in);


if(!iFS.is_open())


throw false;


throw true;


}


catch(bool bCode)


{


return bCode;


}


}





bool ReadData(ifstream%26amp; iFS,char cData[], int%26amp; iSize)


{


try


{


iFS %26gt;%26gt; iSize;


if(iSize %26lt; 0);


else


{


clear(iFS);


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


iFS.get(cData[i]);


clear(iFS);


}


throw true;


}


catch(bool bCode)


{


return bCode;


}


}





void clear(ifstream%26amp; iFS)


{


char c = '\0';


while(c!= '\n' %26amp;%26amp; !iFS.eof())


iFS.get(c);


c='\0';


}








void PrintData(const char cData[], int iSize)


{


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


cout %26lt;%26lt; cData[i];


cout %26lt;%26lt; endl;


}








void sort(char a[], int numberUsed)


{


int indexOfNextSmallest;


int index;





for (index = 0; index %26lt; numberUsed - 1; index++)


{


indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);


swapValues(a[index], a[indexOfNextSmallest]);


}


}





void swapValues(char%26amp; v1, char%26amp; v2)


{


char temp;


temp = v1;


v1 = v2;


v2 = temp;


}





int indexOfSmallest(const char a[], int startIndex, int numberUsed)


{


int min = a[startIndex],


indexOfMin = startIndex;


int index;





for (index = startIndex + 1; index %26lt; numberUsed; index++)


if (a[index] %26lt; min)


{


min = a[index];


indexOfMin = index;


}





return indexOfMin;


}
Reply:check if file2.txt exists OR


check permissions also OR


take different variable of ifstream OR
Reply:Hey read it here





http://www.chris.spear.net/pli/fileio.ht...
Reply:I just spent a while messing round with this and it took me a bit of time, but i figured out what's gone wrong. As far as I can tell the "close" method, doesn't completely reset the ifstream class, so when you open a new file with the same ifstream instance, it still has its "eof" bit set. You can fix it, by adding:





T.clear();





After your first call to:





T.close();





At least, that fixes it on my version (C++.Net 2003)


How do I make it so this C++ program won't go into an infinite loop?

I'm not used to using character values in my loops. For some reason it goes a little while. It may not be infinite, I never waited long.





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





char builder_name[35];


char choice;





void add_builder()


{





while(choice != 'N' || choice != 'n')


{





ofstream builders;


builders.open ("builders.txt", ios::app);





cout %26lt;%26lt; " Enter the name of a builder: ";


cin.get(builder_name, 35);


cin.ignore();





builders %26lt;%26lt; builder_name %26lt;%26lt; endl;





builders.close();





cout %26lt;%26lt; " Would you like to add another builder? (enter (Y) or (N): ";


cin %26gt;%26gt; choice;





};





}





int main ()


{





add_builder();





return 0;





}

How do I make it so this C++ program won't go into an infinite loop?
while(choice != 'N' || choice !='n')


In this line and (%26amp;%26amp;) should be used instead of or(||).


Because if or( || ) is used, atleast one condition will be true for any input (n or N). so loop will go infinite. Hence we should use and(%26amp;%26amp;) to make the loop to terminate for either of the input n or N.


Semicolon should not be used at end of while loop.
Reply:Change the line:


while(choice != 'N' || choice != 'n')


Into:


while(choice != 'N' %26amp;%26amp; choice != 'n')
Reply:Here's your problem - while(choice != 'N' || choice != 'n').


It's that ol' debbil double negative, in a different context. As long as choice isn't both N and n, it loops. If choice ==N, choice !=n is true and vice versa.

edible flowers

What am I doing wrong in this C++ program?

[Linker] error undefined reference to `meanCalc(int, int)'





#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int meanCalc(int,int);








int main ()


{


int array[25];


int count = 0;


int mean;








ofstream fout;


ifstream fin;





fin.open("lab1.txt");


if(!fin)


{


cout%26lt;%26lt;"lab1.txt FAILED TO OPEN"%26lt;%26lt;endl;


cout%26lt;%26lt;"PLEASE CHECK THE FILE AND TRY AGAIN";


}














do {


fin%26gt;%26gt;array[count];


count++;


} while (fin);


count-=2;





mean = meanCalc (array[25],count);


cout%26lt;%26lt;mean;





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


{


cout%26lt;%26lt;array[i]%26lt;%26lt;endl;


}











system ("PAUSE");


return 0;


}





int meanCalc (int (array)[25], int count)


{


int x;


int mean;





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


{


x = array[i] + array[i++];


}





mean = x/count;





return (mean);


}

What am I doing wrong in this C++ program?
int meanCalc(int,int);





doesn't jibe with:





int meanCalc (int (array)[25], int count)





first takes two ints as input, second takes array of ints and an int. you need to reconcile that. i would modify this int meanCalc(int,int); accordingly.
Reply:i#include%26lt;iostrem.h%26gt;


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


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


What is wrong with this code ? its c++?

#include %26lt;iomanip%26gt;


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;


int main()


{


float costtotal,totalcost=0;


int ordercount,I=0;


float averagecost,avg;


void determinecost (float,int,float%26amp;,int%26amp;);


determinecost(totalcost,I,totalcost%26amp;,I%26amp;)...


costtotal = totalcost;


ordercount = I;


void calcaverage(float,int,float%26amp;);


averagecost=calcaverage(costtotal,orderc...


ofstream outfile ("a:\\PIZZACOST.txt");


outfile%26lt;%26lt;setw(20)%26lt;%26lt;"The Average cost is: "%26lt;%26lt;setprecision(4)%26lt;%26lt;showpoint;


outfile%26lt;%26lt;dec%26lt;%26lt;averagecost%26lt;%26lt;"\n";


cout%26lt;%26lt;"DONE......Check file";


return 0;


}


void determinecost (float totalcost,int I,float%26amp; totalcost, int%26amp; I)


{





int totalpizzaorders = 0;


int totallargepizzas = 0;


int totalmediumpizzas = 0;


int totalsmallpizzas = 0;


int accnum;


char pizzasize;


char pizzatype;


float cost;


ifstream infile("a:\\PIZZAORDER.txt");


ofstream outfile ("a:\\PIZZACOST.txt);

What is wrong with this code ? its c++?
you havn't closed "void determinecost (...)" function with curly braces " } "





in c++, you have to close any block of code with } that has been opened with {
Reply:Adding the compiler error message would make answering the question much easier too!





But yes, the final curly brace does appear to be missing - unless you simply missed copying it.


Sunday, July 12, 2009

Ok still have a problem with this C++ program?

// This program by Dillon Nicholson gets numbers by the user


// then counts the number of digits in the number either being both


// positive or negitive whole numbers





#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;


using namespace std;





void countingFunction(int);





int main()


{


ofstream out;


out.open("output.txt");





int number, num;





out%26lt;%26lt;"This is the program of : ";


out%26lt;%26lt;"Dillon Nicholson"%26lt;%26lt;"\n";


out%26lt;%26lt;"CMPS 1043-02"%26lt;%26lt;"\n";


out%26lt;%26lt;"Due date:";


out%26lt;%26lt;" Nov 15 2007"%26lt;%26lt;"\n"%26lt;%26lt;"\n";


cin%26gt;%26gt;number;


cin%26gt;%26gt;num;


countingFunction(number);


out%26lt;%26lt;num;


out.close();


return 0;


}





void countingFunction(int num, int count)





{


while (num != 0)


if (num %26lt; 0) num*=-1;


else if (num %26gt;= 10)


count = 1;


count++;


num= num / 10;


}








its got an error, im sorry for reposting but i made alot of changes, but its spose to allow me to imput a number then count the digits in it, please ne advice or correction of code would be great

Ok still have a problem with this C++ program?
remove line 24: cin%26gt;%26gt;num





why would you capture an input to the result?





line 9 should be:





void countingFunction(int, int%26amp;);





line 25 should be:





countingFunction(number, num);





line 31 should be;





void countingFunction(int num, int %26amp;count)





you need to pass by reference or the count will not pass back.
Reply:modify main() to:





int number=0;


cin%26gt;%26gt;number;


int count=1;


if (number %26lt; 0) number *= -1;


while ((number/=10) != 0) count++;


cout%26lt;%26lt;count;


return 0;





Done :).





Update: That should be a cinch:


int countingFunction(int number)


{


int count=1;


if (number %26lt; 0) number *= -1;


while ((number/=10) != 0) count++;


return count;


}


and call countingFunction(number) from your main().


Can someone help me with a c++ program please?

hey i have this program thats supposed to read in info about students from a file organize and print it out, then read in some more info and calculate the gpa and print that out as well.





I have the program done, but for some reason it doesnt work, when i run it it outputs this:





record #0 from readdata:


from SetAll





it prints that out 20 times from numbers 0-19, the only error that the compiler gives me is


warning C4244: '=' : conversion from 'double' to 'int', possible loss of data





Im going to post my program in chunks since it spretty big someone help plz





#include%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;fstream%26gt;





using namespace std;





class Course{


string name;


char grade;


int hours;


public:


Course(){


name="";


grade=' ';


hours=0;


}


Course(string n, char g, int h){


name=n;


grade=g;


hours=h;


}

Can someone help me with a c++ program please?
*looking up*


That is nice of you to try to help this guy, but you are providing Java solutions to a C++ problem. Doh!





EDIT:





This is fairly hopeless right now. If you look closer, you have pasted a lot of your code with lines that are truncated. (Search for the lines ending in "..."). I made a quick attempt to copy and paste your program, but it won't even compile (of course) as posted. If you address this there is a strong possibility that I will be able to help you fix this once and for all.





Edit #2 - Too funny! I offer to give a guy help, and someone gives me a "thumbs down". Beautiful!


Oh, and looking upwards again...


You consider it anal when I question when a guy puts out code in C++, and Java solutions that aren't necessarily even pertinent to his problem were provided? It is my opinion that you might be dwelling on the compiler "warning" that he incurred, which might not even be remotely related to his *problems*. He is apparently suffering far worse complications than some loss of precision from what he printed out. It looks like he is printing empty gibberish without any real results at this point. You are correct in what you have written, but this doesn't seem to be his worst issue from what he has written.


By the way, no offense or personal malice toward the guy on top, OK? I can now see that you are competent and not just some rube that copied and pasted from Sun's website.
Reply:DISCLAIMER: I use Java syntax and code in my answer, but it should be relatively the same in C++ (except for when i use parseDouble(). I write this disclaimer because some people are anal (*cough* the answerer below me *cough*). I know all too well that the program is in C++. Not stupid.





***************





In general, the way to fix this is to find the line of code throwing the error and cast (as an integer) the double variable you're attempting to assign to an integer. The rule of thumb here is that you can't assign an int to a double without some modification, or without a loss of precision.





Like this... (the (int) is where I'm doing the casting)





int test = (int) variable





(where variable is any double number).





Example:





THIS IS WRONG.


*******************


double testDouble = 2.3;


int test = testDouble;


*******************





THIS IS RIGHT.


*******************


double testDouble = 2.3;


int test = (int)testDouble;


*******************





You might also have to do something more complex, like





*******************


double testDouble = 2.3;


int test = Int.parseInt(testDouble);


*******************





(that was just an example, in that case, you wouldn't need to use parseInt(), a (int) caste would be sufficient.)





It all depends. Basically, though, you need to get the left side (integer) to be of the same as the right side. The way you have it, the right side is too complex for a much less complex integer, explaining why the compiler is yelling at you.

covent garden

Can someone help me with a c++ program please?

hey i suck at this, i need to write a program that plays the game hangman, i wrote some of it, not even sure if any of it is right though.


I'll post the programs and then I'll ask questions.





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;











int main(){


string myWords[1000];


short wordCount=0;


ifstream word;


word.open("dictionary.txt");


while (!word.eof()){


word%26gt;%26gt;myWords;


wordCount++;


}


int x = rand() % wordCount + 0;


string question = myWords[x];








return 0;


}











void drawGallows(char a[]){


char gallows[10]{





" //========= ",


" || ",


" || ",


" || ",


" || ",


" || ",


" || ",


" || ",


"/||\ ",


"

Can someone help me with a c++ program please?

Reply:__________";


}
Reply:Well, you seem to have your gallows on the right track, so I'll omit that. My example uses streamstreams in lieu of a file since I didn't feel like creating a file (and you already have the code for that).





For the answer, I'm taking the string's length and turning all its characters into asterisks. Once a letter is found, the string 'ourword' character is also turned into an asterisk. Once all the characters in 'ourword' are asterisks, then it matches the answer, ie. * * * * * *.





[code]





#include %26lt;string%26gt;


#include %26lt;sstream%26gt;


#include %26lt;iostream%26gt;





int found;


string ourword; //for example, copies an array word for puzzle


string answer; //used for comparison


int wrong=0; //must initialize this for increment





void startgame()


{


std::cout%26lt;%26lt;"
Reply:||\n"; //build this function to display hangman


}





//////////////////////////////////////...


string%26amp; FindChar(string%26amp; look_for_letter,


const string%26amp; findMe) {


//start at pos 0


size_t letter = look_for_letter.find(findMe, 0);


// look for letter


if (letter != string::npos) //string, find and replace function


found=1;


else


found=0;


}


//////////////////////////////////////...





string letter; //we declare this globally since it's used both


//inside and outside of call_for_letter function.





void call_for_letter()


{


letter.erase();


std::cout%26lt;%26lt;"please enter a letter";


std::cin%26gt;%26gt;letter;


}





std::string sentence="I am a string and I want to add myself into"


" an array ";


std::stringstream as; //in lieu of reading from file I'm using a


//stringstream - I'm declaring that here.





std::stringstream ar; //we'll use this stream to buffer sentence


//for reading into an array





std::string word; //our string word once found


std::string fullmonty[1024]; //size limit


std::string billy [13]; //our array size -- arrays begin at 0, not 1!


std::string entry; //our string to store underscore display


std::string underline="_"; //character to use for underscore


std::string asterisk="*"; //character to use for asterisk





int main ()


{


as %26lt;%26lt; sentence; //insert our sentence of words into stringstream


ar %26lt;%26lt; sentence;


for (int n=0 ; n%26lt;13 ; n++ ) //n12 - do not exceed array size


{


std::getline(as,word,' '); //word stops at whitespace


billy[n]=word; //begin inserting words into array beginning at 0


//as for loop advances, so does our array #





std::cout %26lt;%26lt; billy[n]%26lt;%26lt;std::endl; //print each array at its stored


}


std::cout%26lt;%26lt;std::endl;//for display spacing


std::cout%26lt;%26lt;billy[3]; //for show that array is stored.


std::cin.get(); //pause to look


ourword=billy[3]; //copy this array to ourword for puzzle word





//make underscores for display (entry string)





for (int y=0; y%26lt;ourword.length(); y++){ //use array.string


//length to determine how many underscores should be inserted


// into empty 'entry' string. Same for asterisks





entry.insert(0,underline,0); //insert underscores


answer.insert(0,asterisk,0); //insert asterisk *use to compare


//a completed answer





}





std::cout%26lt;%26lt;std::endl;


std::cout%26lt;%26lt;entry; //displays underscore (word length)





std::cin.get();





//add entire sentence into an array





std::getline(ar,word);


fullmonty[1024]=word;





std::cout%26lt;%26lt;std::endl;


std::cout%26lt;%26lt;fullmonty[1024];


std::cin.get();





//let's start a game





while (letter!="quit"){ //so long as user doesn't type in 'quit'


startgame();


call_for_letter(); //call our input function





FindChar(ourword,letter); //look for letter


string::size_type loc= ourword.find(letter); //id location





if( loc!= string::npos )


{


ourword.replace(loc,1,"*");


entry.replace(loc,1,letter);





std::cout%26lt;%26lt;std::endl;


std::cout%26lt;%26lt;ourword; //we replaced our found letter with an asterisk


std::cout%26lt;%26lt;std::endl;


std::cout%26lt;%26lt;entry; //above, we've replaced an underscore with letter found


startgame(); //return to input another letter


}





else{


std::cout%26lt;%26lt;"letter not found";


//here, you would handle wrong answer, count them,


//change hangman configuration in startgame strings.


wrong++; //keep track of wrong letters


std::cin.get();


startgame(); //return to input another letter if wrong answers are below min.


}





if (ourword==answer)


{


std::cout%26lt;%26lt;"You win!";


cin.ignore();


cin.get();


return 0;


}





}





return 0;


}


[/code]
Reply:I think you can consult a freelancers to get your code working


from


http://expert.myitcareer.org/


I need some help with a C++ program?

Hey I keep getting errors in this program and im not sure how to fix them, the program is supposed to tally up votes and print out winners in 6 different races. I'll post the program first and then my question:








#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;


using namespace std;


void tally(int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;,int%26amp;...


void print(int, int, int, int);





int main() {





cout%26lt;%26lt;"CS-114 Ballot Counter"%26lt;%26lt;endl%26lt;%26lt;endl;








cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Presidency:"%26lt;%26lt;endl;


print(1, 2, p1, p2);





cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Vice Presidency:"%26lt;%26lt;endl;


print(3, 4, vp1, vp2);





cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Secretary:"%26lt;%26lt;endl;


print(5, 6, s1, s2);





cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Treasurer:"%26lt;%26lt;endl;


print(7, 8, t1, t2);





cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Judge:"%26lt;%26lt;endl;


print(9, 10, j1, j2);





cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Here's the race for the Senator:"%26lt;%26lt;endl;


print(11, 12, sen1, sen2);





}

I need some help with a C++ program?
Undeclared identifier means you're using a variable that you haven't declared yet, and the compiler doesn't know what it is since you haven't told it. You need to declare p1, p2, vp1, etc before you ask the computer to print them. So at the very top of your main(), you need this statement:





int p1, p2, vp1 .... sen1, sen2;





You probably need to initialize all of them to zero too before you start; otherwise they will contain garbage values.





Next, if your program really executes in that order, I think you're trying to print the results before you actually collect them, so you need to open ballot.txt and tally up the votes before you call print(1, 2, p1, p2) etc.





Also, you're getting voter %26gt;%26gt; vote before your while loop, then doing it again once you get inside the loop, so you're always going to miss the first vote, unless the first entry in ballot.txt is a dummy value. You should delete the first voter %26gt;%26gt; vote; statement. If the file's empty, you'll get an error there anyway.





Your big long if block should be a switch-case statement instead of a series of if's. I believe the syntax is like this:





switch(vote)


{


case 1: p1++; break;


case 2: p2++; break;


default: cerr %26lt;%26lt; "Invalid vote";


}


The default case is for when none of the other cases match. You currently have p1+=0 for that, which makes no sense. You should just do nothing, or at least output an error message.





Good luck with your program!

email cards

I need a program in c++ that read an archive?

The archive is like this: avemaria { anormal } . Ok I need that the program separate the word "anormal" and put it in a queue.


This is what I got:


#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;


#include%26lt;cassert%26gt;


#include%26lt;string%26gt;


using namespace std;





int main()


{


ifstream archivo;


string reading,


palabras;





getline(cin, palabras);


archivo.open(palabras.data());


assert( archivo.is_open() );





for (;;)


{


archivo %26gt;%26gt; reading;





if ( archivo.eof() ) break;


if ( reading == "{")


cout%26lt;%26lt;reading;


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


}





system("pause");


}





Please, help me.


Thanks

I need a program in c++ that read an archive?
Read the string. When you get to the "{", read the following characters into the queue until you get to the "}". (It's called a state machine, and this one is trivially simple.)


How would i do this c++ program without using boolean?

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


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


#include %26lt;ctype.h%26gt; // needed for toupper


#include %26lt;stdlib.h%26gt;// needed for rand


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





using namespace std;





int main ( )


{





int num[20]={0};





const int numLength = 20;





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


{


cout%26lt;%26lt;"enter 20 numbers with


repeating numbers"%26lt;%26lt;endl;


do{


cout%26lt;%26lt;"enter number "


%26lt;%26lt;(count+1)%26lt;%26lt;": "%26lt;%26lt;endl;


cin %26gt;%26gt; num[count];


}


while (num[count]%26lt;10


|| num[count] %26gt; 100);





}


cout %26lt;%26lt; "The original set of numbers were: ";





for(int countfun = 0;countfun


%26lt; 20;countfun++)


{


cout %26lt;%26lt; num[countfun]


%26lt;%26lt; setw(3);





}





cout %26lt;%26lt;"\nThe different numbers


from the set of integers are: ";





return 0;


}





I'm supposed to have 20 random numbers with repeating variables.





Example: My numbers 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2





Original set of numbers: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2


New set of numbers: 1 2





I can't use boolean to remove the duplicates. How would I do this? Thanks.

How would i do this c++ program without using boolean?
After you assign a number into the array, test whether that number is equal to any other array member. If it is a match, then do not increment to the next array member, because you want to assign the next number into that same array member. If it is not a match then you assign the next number to the same array member.





For example, the value of num[0] is 1 and you assign 1 into another array member, here's the test:


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


if (num[i] == num[0]) count--;





Now you might have another problem, because each time you decrement "count", your other loop "for(int count = 0;count %26lt; 20;count++)" will run an extra iteration. That problem can be solved by using a variable such as "max" instead of the literal number "20", and decrementing "max" when you decrement "count". So now the test will look like this:


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


if (num[i] == num[0])


{


count--;


max--:


}
Reply:This sounds like a class project, so I'm not going to give you any code so that you won't be accused of cheating. That being said, here are a couple of solutions.





Get the instructor to define "can't use boolean." Are you not allowed a boolean variable? What about a method that returns a boolean?





If you can use a method, that is how I would do it:


bool duplicateFound(int* original_set, int num_to_check)





if that is not allowed, remember that in C++, boolean is really an integer. False is 0, anything else is true. But this is really cheating.





How about a recursive method that returns the duplicate?





int recursiveCheck(int* num_to_check, num)


{


//check the first number and you are not at the end of the array


// if check fails, increment the pointer and call this function


}


Creating a simple menu in C++?

I want to create a very simple menu that one option to allow me to load a input file and another option to display the input file and the last option to quit the program.. Is there any resources that can help me with this?





I have this so far:





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


int l, d, q;


char ch;


ofstream outFile;


string outputFile;





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


cout %26lt;%26lt; "L Load from file";


cin %26gt;%26gt; ch;


cout %26lt;%26lt; endl;





outFile.open("sample.txt", ios::out);}





I compile it successfully but running it was kinda messed up (figured that anyway since it's not completely done).

Creating a simple menu in C++?
it is an example of implementng menu urs dont has any menu concept.








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


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





void main(void)


{


int num1, num2, operation, result;





clrscr();


printf("Enter a number: ");


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


printf("Enter a number: ");


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





printf("Indicate math operation\n[1:add; 2:sub; 3:mul; 4:div]: ");


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





switch(operation){


case 1:


result = num1 + num2;


break;


case 2:


result = num1 - num2;


break;


case 3:


result = num1 * num2;


break;


case 4:


result = num1 / num2;


break;


default:


result = 0;


}





printf("\nThe result is %d", result);


}


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.

cheap flowers

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?


Someone help me with this c++ program please?

ok I have a program that reads in values from a file which contains a bunch of numbers, it tallies these numbers as votes in 6 different races and prints out the number of votes for each candidate as well as the winner and the percentage of votes, i need to rewrite the program using arrays which i dont know how to do, heres the program:


#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() {


int p1=0,p2=0,vp1=0,vp2=0,s1=0,s2=...


int vote;








ifstream voter;


voter.open("ballot.txt");














while(!voter.eof()){

Someone help me with this c++ program please?
Think of an array as a row in an excel worksheet. Each cell is an array position. Each position has the same datatype. So, if you have 12 candidates, you could keep track of their votes in an array as follows:





int candidates[12];





Arrays always start at position 0. Suppose you have Fred and Joe as candidates. Fred can be candidate[0] and Fred is candidate[1]. Then you can use them just as any other int. For example, to add one to the vote count for Fred, just do this:





candidate[1]=candidate[1]+1;





(Or shorter, candidate[1]++; )





There's more to arrays, but this should get you going.


Help me with a c++ program please?

hey i need a little help with a program, ill post part of the program and then ask my question





#include%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;fstream%26gt;





using namespace std;





class Course{


string name;


char grade;


int hours;


public:


string ("") in the default constructor.


Course(){


name = "";


grade = "";


hours = 0;


}


Course(string n, char g, int h);





void setName(string n){name=n};


void setGrade(char g){grade=g};


void setHours(int h){hours=h};





string getName(){return name};


char getGrade(){return grade};


int getHours(){return hours};





void print(){


if (name != ""){


cout.width(10); cout%26lt;%26lt; name;


cout.width(4); cout %26lt;%26lt; grade;


cout.width(4); cout %26lt;%26lt; hours %26lt;%26lt; endl;


}


else


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


}


};


class Student{


string last,first,street,city,state,z...


int id;


int num_classes;


Course classes[15];


int hours_att, hours_earned, grade_points;


double gpa;

Help me with a c++ program please?
WOW!! Way out of my league im barely learning C++,





but you think you can help me, You look like you know way more than i do. Look at my questions please?


Help me with a c++ program please?

hey i need a little help with a program, ill post part of the program and then ask my question





#include%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;fstream%26gt;





using namespace std;





class Course{


string name;


char grade;


int hours;


public:


string ("") in the default constructor.


Course(){


name = "";


grade = "";


hours = 0;


}


Course(string n, char g, int h);





void setName(string n){name=n};


void setGrade(char g){grade=g};


void setHours(int h){hours=h};





string getName(){return name};


char getGrade(){return grade};


int getHours(){return hours};





void print(){


if (name != ""){


cout.width(10); cout%26lt;%26lt; name;


cout.width(4); cout %26lt;%26lt; grade;


cout.width(4); cout %26lt;%26lt; hours %26lt;%26lt; endl;


}


else


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


}


};


class Student{


string last,first,street,city,state,zip,major,m...


int id;


int num_classes;


Course classes[15];


int hours_att, hours_earned, grade_points;


double gpa;

Help me with a c++ program please?
can you tell me what happens when you run it

baseball cards

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.


I am using c++ with doubles. say the number is 987654321, it changes it to 987650000. anyone know why? fix?

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


#include %26lt;iostream%26gt;


using namespace std;


#include %26lt;string%26gt;


#include %26lt;fstream%26gt;


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


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





void main()


{


double ktemp=987654321.123456789;


cout %26lt;%26lt; ktemp %26lt;%26lt; endl;


}





it will display 9.87654e+008, and if I divide it by 1000 it shows 987654. this is a huge problem since im dealing with GIS coordinates and I NEED the full value of the number, i get the same problem when writing these values to a file, which is my ultimate goal

I am using c++ with doubles. say the number is 987654321, it changes it to 987650000. anyone know why? fix?
try





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


{


double ktemp=987654321.123456789;


cout.precision(20); // this sets the precision of the output to 20 places.


cout %26lt;%26lt; ktemp %26lt;%26lt; endl;


cout %26lt;%26lt; ktemp/1000. %26lt;%26lt; endl;





return 0;


}


Why does this c++ program go into an infinite loop?

This program keeps going into an infinite loop for some reason. When I delete everything from ofstream builders; to builders.close(); cin.ignore(); it doesn't go into the infinite loop. So the eroor is somewhere in there.





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





char builder_name[35];


char choice;





void add_builder()


{





while(choice != 'N' %26amp;%26amp; choice != 'n')


{





// error is somewhere between here





ofstream builders;


builders.open ("builders.txt", ios::app);





cout %26lt;%26lt; " Enter the name of a builder: ";


cin.get(builder_name, 35);


cin.ignore();





builders %26lt;%26lt; builder_name %26lt;%26lt; endl;





builders.close();


cin.ignore();





// and here





cout %26lt;%26lt; " Would you like to add another builder? (enter (Y) or (N): ";


cin %26gt;%26gt; choice;





}





}





int main ()


{





add_builder();





return 0;





}

Why does this c++ program go into an infinite loop?
Move the 2nd cin.ignore() to after cin %26gt;%26gt; choice;





In response to the other answerer, your logic in the while loop is fine.





(choice != 'N' %26amp;%26amp; choice != 'n')





If choice is Q, y or anything then it is both != 'N' and !='n' (true %26amp;%26amp; true : true)





If choice is N then the conditions is true %26amp;%26amp; false which is false, and the condition fails, ending the loop.





If the choice is n, then the condition is false %26amp;%26amp; true which is false, and the condition fails, ending the loop.





I think the third answerer is confusing "%26amp;%26amp; and" with "|| or"





while(choice != 'N' || choice != 'n') (this will be an infinite loop)
Reply:The while loop never breaks.


and found the mistake 1st U said


while(choice != 'N' %26amp;%26amp; choice != 'n')


This is impossible instead try this


while(choice != 'N' || choice != 'n')


2nd


cout %26lt;%26lt; " Would you like to add another builder? (enter (Y) or (N): ";


cin %26gt;%26gt; choice;


\\write this


if(choice == 'N' || choice == 'n'){


break();


}else{


\\name of that Function or declartion
Reply:because you put "return 0 ;" that makes it return to the begining of the code. and the semi colon makes it a command. at least i think thats what it is.


In my c program i can't do any remove or rename to file is it because i openit and close in each function?

or what is prevent the rename and remove


i include fstream

In my c program i can't do any remove or rename to file is it because i openit and close in each function?
Of course yo can't.


Every file in C:\program files is very important to your computer and some of them are still running.


Trying to remove or rename them will let your computer get crash easily.


If you just want to remove the program you don't want, simply go to "add or remove program" to remove it. Don't even try to rename a file in the directory, or c:\Windows or C:\ Documents and Settings.
Reply:you can try using a system call() or you could copy the file and then paste it into a new one. you could try using this line as well:





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


int rename( const char *oldfname, const char *newfname );





The function rename() changes the name of the file oldfname to newfname. The return value of rename() is zero upon success, non-zero on error.





And to the guy above me she didn't ask for you to do the problem she just wanted to know why she wasn't able to rename or remove files before you call people lazy she did take the time to enter it into yahoo answer to get help do you actually believe she just sat there and waited for a response?


idiot
Reply:There is no rename/remove in fstream. And if you include fstream, that means you are in C++, not C. Do you not even know what language you are programming in? Doesn't matter in the end.





Renaming and removing files is an OS dependent action. Perhaps the simplest thing you could do is issue a system() call with the appropriate rename/delete commands. The more efficient way, but you seem incapable of accomplishing this, is to include the OS specific header files and use the API to rename and delete. You can find out how through MSDN for Windows, and the man pages for Linux.





If you were looking for people to do your homework for you, don't bother. Seriously, don't. No real programmer will have pity on you. We all our work-oriented people. If you're too lazy to Google and try things on your own, we'll leave you to your imminent failure.
Reply:You can't rename or remove file if it is open. File should be closed before such operations.

artificial flowers

Intro to C++ Problem please help!?

My inFile will be something like this





6


1 2 3 4 5 6


0 1000 1 0 -999 1


-2 1 -3 -1 2 3


-10 -30 -20 -5 -15 -50





so, the first number is the number of numbers in each line.





I have to calculate the ave, min, max and total for each line and outfile it like this;





1 2 3 4 5 6


Min: 1


Max: 6


Ave: 3.5


Total: 21





0 1000 1 0 -999 1


MIn: ....





and so on.





My problem so far is that I cant find out how to inFile the first number into a variable, then input the following numbers of each line into variables, here is what I have so far;





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;


#include%26lt;iomanip%26gt;


#include%26lt;fstream%26gt;


using namespace std;


int main ( )





{


int j,i;





ifstream inFile;


ofstream outFile;





inFile.open("inFile.txt");


outFile.open("outFile.txt");





if((inFile.fail())||(outFile.fail()))


cout%26lt;%26lt;"Error: File open failure"%26lt;%26lt;endl;


else


{


do


{


if(j!='\n')


{


inFile%26gt;%26gt;j;


}


for(i=0; i!='\n', i%26lt;=j; i++)


{


inFile%26gt;%26gt;i;


}


cout%26lt;%26lt;setw(50)%26lt;%26lt;i%26lt;%26lt;endl;


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


}


while(!inFile.eof());


}


return 0;


}

Intro to C++ Problem please help!?
Here is the solution





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


#include %26lt;sstream%26gt;


#include %26lt;algorithm%26gt;


using namespace std;


ifstream inFile("inFile.txt");


#define cin inFile


ofstream outFile("outFile.txt");


#define cout outFile


int main()


{


string line;


int n,total,index;


int *elements;


cin%26gt;%26gt;n;


getline(cin,line);


elements=new int [n];


while (getline(cin,line))


{


istringstream ss(line);


index=0;


while (!ss.eof())


ss%26gt;%26gt;elements[index++];


cout%26lt;%26lt;"Min: "%26lt;%26lt;*min_element(elements,elements+n)%26lt;%26lt;en...


cout%26lt;%26lt;"Max: "%26lt;%26lt;*max_element(elements,elements+n)%26lt;%26lt;en...


total=0;


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


total+=elements[j];


cout%26lt;%26lt;"Ave: "%26lt;%26lt;(float)total/n%26lt;%26lt;endl;


cout%26lt;%26lt;"Total: "%26lt;%26lt;total%26lt;%26lt;endl%26lt;%26lt;endl;


}


delete []elements;


return 0;


}





hope i helped u


Streamreader visual c++ question!?

I want to read from a text file for my MFC application. I used streamreader but visual studio says it's undeclared.





I tried fstream as well and i'm getting undeclared errors too. Any idea why?





Is streamreader the best way to read a text file line by line? Thanks.

Streamreader visual c++ question!?
well I'm not sure but isn't Streamreader in C#?





If you;re trying to read in a text file then you must include the library %26lt;fstream.h%26gt;





and the line of code is





string line;


ifstream myfile ("example.txt");


while (! myfile.eof() )


{


getline (myfile,line);


cout %26lt;%26lt; line %26lt;%26lt; endl;


}


myfile.close();


}


Help with c++ program please!?

im supposed to use arrays for this program in the function tally and function print





heres what i have so far


#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;


using namespace std;


void tally(int votes[12]);


void print(int, int, int votes[12]);





int main() {


int votes [12];


votes[0]=0;


votes[1]=0;


votes[2]=0;


votes[3]=0;


votes[4]=0;


votes[5]=0;


votes[6]=0;


votes[7]=0;


votes[8]=0;


votes[9]=0;


votes[10]=0;


votes[11]=0;





int p1=0,p2=0,vp1=0,vp2=0,s1=0,s2=0,t1=0,t2=...


int vote;








ifstream voter;


voter.open("ballot.txt");














while(!voter.eof()){








voter%26gt;%26gt;vote;





if (vote==1){


p1+=1;}


if (vote==2){


p2+=1;}


if (vote==3){


vp1+=1;}


if (vote==4){


vp2+=1;}


if (vote==5){


s1+=1;}

Help with c++ program please!?
Didnt look through everything, but one problem is call print this way





print(3, 4, votes);


not


print(3, 4, votes[12]);


Help with c++ please?

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


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


#include %26lt;ctype.h%26gt; // needed for toupper


#include %26lt;stdlib.h%26gt;// needed for rand


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





using namespace std;





int main ( )


{


int months[12]={31,28,31,30,31,30,31,31,30,3...


int month[2]={0,0};


int dan[2]={0,0};


int daystart=0;


int munth=0;


int latot=0;


int year=-1;





cout %26lt;%26lt; "Enter starting month #: ";


cin %26gt;%26gt; month[0];


while(month[0]%26lt;0 || month[0]%26gt;12)


{


cout %26lt;%26lt; "Wrong number, try again: ";


cin %26gt;%26gt; month[0];


}





cout %26lt;%26lt; "Enter starting day #: ";


cin %26gt;%26gt; dan[0];


while(dan[0]%26lt;0 || dan[0]%26gt;31)


{


cout %26lt;%26lt; "Wrong number, try again: ";


cin %26gt;%26gt; dan[0];


while (month[0]==2 ||


month[1]==2 %26amp;%26amp; dan[0]%26gt;28)


{


cout %26lt;%26lt; "Wrong number, try again: ";


cin %26gt;%26gt; dan[0];


}


}





cout %26lt;%26lt; "Enter ending month #: ";


cin %26gt;%26gt; month[1];


while(month[1]%26lt;0 || month[1]%26gt;12)


{


cout %26lt;%26lt; "Wrong number, try again: ";


cin %26gt;%26gt; month[1];


}

Help with c++ please?
No.





Do your own homework, or ask a specific question if you are stuck.

800flowers.com

Help with c++ hw?

how do i write a program that reads data from that file and stores the output in a file named “OutData.txt”. For each employee, the data must be outputted in the following form: firstName lastName updatedSalary. Format the output of floating point numbers to be always displayed using the decimal notation with two decimal places.





here is what i have s far:





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int main()


{


ifstream input;


ofstream output;





input.open("InData.txt");


____________________________


what do i do after this point? can u please show some examples?

Help with c++ hw?
You don't say how the input data file is formatted.


Your code is good so far. Continue with:





output.open( "OutData.txt" );


output.precision( 2 );


string firstName, lastName;


double salary;


input %26gt;%26gt; firstName %26gt;%26gt; lastName %26gt;%26gt; salary;


while (!input.fail()) {


output %26lt;%26lt; firstName %26lt;%26lt; " " %26lt;%26lt; lastName;


output %26lt;%26lt; " " %26lt;%26lt; fixed %26lt;%26lt; salary %26lt;%26lt; endl;


input %26gt;%26gt; firstName %26gt;%26gt; lastName %26gt;%26gt; salary;


}





The "precision" function says you want 2 digits. The "fixed" manipulator says that you always want those digits counted from the decimal place.


Help with c++ program please?

ok I have a program that reads in values from a file which contains a bunch of numbers, it tallies these numbers as votes in 6 different races and prints out the number of votes for each candidate as well as the winner and the percentage of votes, i need to rewrite the program using arrays which i dont know how to do, heres the program:


#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() {


int p1=0,p2=0,vp1=0,vp2=0,s1=0,s2=...


int vote;








ifstream voter;


voter.open("ballot.txt");














while(!voter.eof()){

Help with c++ program please?
Well its been a while since I used C++ but the way you can do it logically is to just add each vote to the array index. Just take the first number and add it in to the 0 index. Then the second number into the 1'st index ect.


add it by:





int x = 0;





while(x%26lt;array.length())


{


array[x] = numberOfVotes;


x++;


}





with the print.








int y = 0;





while(y%26lt;array.length())


{


cout%26lt;%26lt;array[y];


}


In C++, how would you prevent it from writing over a line in a file?

How would you prevent it from writing over a line in a file? For example, whenver I run this program and type multiple things in, in just keeps on writing over the same line, instead of making a new one for each input. Can somebody show me how to prevent this?





#include %26lt;iostream%26gt;


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


#include %26lt;string%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;fstream%26gt;


using namespace std;


int main()


{


cout %26lt;%26lt; "Please enter the person whom you are voting for." %26lt;%26lt; endl;


string name;


cin %26gt;%26gt; name;


if (name == "exit") {


return 0;


}


else {


ofstream outfile("votinglog.txt");


outfile %26lt;%26lt; name %26lt;%26lt; "\n" %26lt;%26lt; endl;


cout %26lt;%26lt; "Thank you, your vote has been received. \a " %26lt;%26lt; endl;


outfile.close();


return main();


}


}

In C++, how would you prevent it from writing over a line in a file?
Look at http://www.cplusplus.com/reference/iostr... . You'll see that you want to open up the file for appending. Hence, you should do something like outfile("votinglog.txt", ios::app)





Other remarks:


- #include %26lt;stdlib.h%26gt; - Deprecated. The correct form is %26lt;cstdlib%26gt; . Which you did anyway. So why %26lt;stdlib.h%26gt;?


- return main(); - What is this supposed to be? An attempt at a recursive function? You never call main. It is a special function, defined as an entry point. If you want a loop, use a while loop, set to a condition of true.





And when posting code, use http://www.rafb.net/paste/ or another nopaste / pastebin.


Another C++ Question?

Sort and output an array of inputted characters, based on their number of occurrences (from highest to lowest). What's wrong with this code?





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





int main(void)


{


using namespace std;





char chars[]="abcdefghijklmnopqrstuvwxyz";


int i;


int count[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0...





char inp;





cin%26gt;%26gt;inp;





while(inp !='.')


{


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


{


if(inp==chars[i])


count[i]++;


}


cin%26gt;%26gt;inp;


}





int swap;


char swapchar;





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


{


if (count[i] %26gt; count[i + 1])


{


swap = count[i];


count[i] = count[i + 1];


count[i + 1] = swap;





swapchar = chars[i];


chars[i] = chars[i + 1];


chars[i + 1] = swapchar;


}


}





cout%26lt;%26lt;"Character Count"%26lt;%26lt;endl;








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


{


if(count[i]!=0)


{


cout%26lt;%26lt;chars[i]%26lt;%26lt;" "%26lt;%26lt;count[i]%26lt;%26lt;endl;


}


}





system("pause");





return 0;


}

Another C++ Question?
You put your using statement inside main().
Reply:Well first of all your namespace position is incorrect... it should be after #include%26lt;fstream%26gt;, you don't need fstream since you are not doing any file streaming.





Second, check your braces... { and } i tried fixing your program and it doesnt work actually because of the errors... your swap or as we call it "Bubble sort" is ok... but change the limit from 26 to 25 since you'll be comparing the second to the last. Also, you'll have to put that in another loop since you need to do that 25 times again since you'll only be pushing one lowest number at a time... you should also try considering if there are equal times it appeared.





Note: I didn't fix it to work... i want you to handle that part. ^^, i'm just here to correct a few errors =)

wildflower

My c++ program has some error..so pl correct them... it is a program to count unique words and what these are?

#include%26lt;iostream%26gt; // Allows program to perform input and output


using std::cout; // program uses cout


using std::cin; // program uses cin





#include%26lt;fstream%26gt;


using std::ifstream;








int main() // function main begains program execution





{





ifstream in;


char w[2000][15];


int alphabet[26];


char ch;


int ind = 0. subind = 0;





// initialise the array with zeros





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


alphabet[i] = 0;





//Open the input file


in.open("Text.txt");





//Check the file opened successfully.


if(!in.is_open())


cout%26lt;%26lt;"Unable to open input file."%26lt;%26lt;endl;





// Read the input file character by character


while(in.get(ch))





// if the character is an alphabet, then append the word


if( (ch%26lt;91%26amp;%26amp;ch%26gt;64)||(ch%26lt;123%26amp;%26amp;ch%26gt;96) )





{


w[ind][subind]=ch;


subind++;


}


// end if





// If a space found, move onto a new word





if(ch==' ')


{


w[ind][subind] = '\0';


subind=0;


ind++;


w[ind][subind]='\0';


} // end fi





} // end while





// Find the cout of occurence of words which starts with a letter





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


alphabet[toupper(w[i][0] = 65]++;





int max=0 // to find the max





// Display results





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


if(alphabet[i]!=0)


{


// Display the cout of words that starts with a letter





cout%26lt;%26lt;"\n" %26lt;%26lt; static_cast %26lt;char%26gt; (i+65) %26lt;%26lt;"'s-"


%26lt;%26lt;alphabet[i]%26lt;%26lt;"--";





// Find for the max occurences of a letter


if (alphabet[i]%26gt;alphabet[max])


max=1;


// Display the words that starts with a specific letter





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


if(toupper(w[j][0]==(i+65))


cout%26lt;%26lt;w[j]%26lt;%26lt;", ";


} // end if





// A conclusion comment of the program


cout%26lt;%26lt;"\n\nLetter with that began the most words was "


%26lt;%26lt;static_cast%26lt;char%26gt; (max+65);





// close the input file


in.close();


return 0; // indicate program executed successfully


} // end of function, main

My c++ program has some error..so pl correct them... it is a program to count unique words and what these are?
What is the output? I may be able to analyse depending upon the actual output.





I cant run it here as i do not have any C++ compiler out here