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


}