er, you aren't initialising 'dr'
JameB commented: Good job! +1
VernonDozier commented: Good advice. +21
Er I should have been more clear, there is something besides setprecision() that allows you to "cut off" anything after a certain digit. Google it; you have to learn something on your own :P. I don't like giving away freebies; it takes away the fun in learning.
float a=3.145698;
float b=a;
define b as a; if you only want 3.14, just set the float precision to 2.
Does your variable need to mark where new lines are? If not, you can just append each line to the same variable. If you do, just ad a null character(or a character of your choosing) after each line prior to appending.
Oh good god, I don't even know where to begin:
-why do you need a goto statement? a loop would do just fine
-you are closing a nonexistent brackets
-42 is not the random number you generated
-and you also have improper brackets
-don't forget srand(time(0));
anyways, a simplified code would be like
int main() {
int guess,ans,try;
bool game = false;
try=0
char ag;
while(!game) {
if(try == 0)
ans = rand()%100+1;
cout<<"guess a number";
cin>>guess;
if(ans == guess) {
cout<<"you win! try again?y/n";
cin<<ag;
if(ag == 'y')
try=0;
else
game=true;
}
else
cout<<"wrong answer, try again"
if(tries==4)
game=true;
try++;
}
system("PAUSE");
return 0;
}
this wont tell if the answer is too high or too low, but I'll leave that to you. Good luck.
OH I see what you were doing, thought you were using a structure, not class lol, thought you made two single links >_<.
As far as i can tell, this only a single link list, besides you can just redefine front everytime there is a new first link, it saves you alot of effort later on, and you will print EVERY element in the list because it isn't list->next, it is list that has to equal NULL. Lastly, a plain void function works just fine. Unless you are required to use a const, I would suggest you refrain from doing so, no need to make it harder than it actually is :)
Your function is a bit, no way too complicated >_>, just keep one pointer to track the first link of your list and reference it when you want to print it.
Node *list, *front, *temp;
list = new struct node;
front = list
//your code here, do not modify "front"
list = front; //print from begining
while(list != NULL)
cout<<list->data;
list = list->next;
For the purposes of his project, the program wouldn't need to be nearly as comprehensive. That being said, some fun projects include: Random essay generator, graph constructor(make individual points, connect them, identify if they are a shape or on a single line ect, give information about shape(or line) i.e. perimeter, area, ect), 3d tic tac toe(use your imagination, its quite interesting depending on how far you want to take it), the game of life(no not the board game, search it up), and lastly, a basic animation of an object using characters(think 2d array).
I've done all of these and they took me from either a day to a week. I personally suggest life, but the REG is quite entertaining depending on where you get your word bank and how you define your sentence structure(add past and future tense and it gets way more difficult).
Sorry but I don't get that. You say you should only delete the first node, but then you show it being done in a loop which explicitly deletes all nodes? Am I missing something?
Nishinoran wants to achieve the same result but put the deletion in the node destructor. Then the first node is explicitly deleted by the list client, as you indicate, and the node destructor takes care of the rest of the list.
I wasn't clear, it was a response to rahul; the snippet does exactly what you said it does.
No, you only delete the first node, the proper way to do it is:
while(node->next != NULL){ //node is the start of your list
rec = node; //rec is used to keep track of node
node = node->next; //move node to next link
delete rec; //delete previous link
}
remeber the lists are still assigned to a junk array so they will not equal NULL, but the memory is freed.
*bangs head against wall* how did I miss that?
I use Devc++ as my compiler so i never had to do int main() { code return 0; } so I'm fetching a guess here, but I DO remeber every time I did use it, it would exit out of my program, but it can't hurt to try it, I've frankly never seen a return; for a void function in my life(6 months of coding tbe, not much, I know)
Eh silly me, why do you have all those returns? They are useless.
*for the void functions
Add Temp=NULL in your constructor.
Urgh, I skimmed your code and noticed the abscence of string arrays, have you considered using them? They make comparison so much easier. The are declared in this format:
std:: string array[4];
dont forget to include <string>
If you gave me the code snippet of where you think it is wrong, I might be able to look at it more thoroughly, also, have you tried inserting couts and system("PAUSE");s throughout your code to see where it stops?
Whenever I used .txt files they were just on the desktop, but I'd put them in the source folder to be sure. Sorry but I'm not all that experienced.
I added two new loops to display the name of the lowest and highest selling products. does this look right? (it worked)
highest = 0; for (int count =0; count < typesOfSalsa ;count++) { if (numOfJarsSold[count]> highest) { highest = numOfJarsSold[count]; } } for (int count =0; count < typesOfSalsa;count++) { if (highest ==numOfJarsSold[count]) { cout<<" The highest selling product is: "<<salsa[count]<<endl; } } lowest = 999//some number you will never exceed with an input for (int count =0; count < typesOfSalsa;count++) { if (numOfJarsSold[count] <lowest) { lowest = numOfJarsSold[count]; } } for (int count =0; count < typesOfSalsa;count++) { if (lowest ==numOfJarsSold[count]) { cout<<" The lowest selling product is: "<<salsa[count]<<endl; } }
I'm not sure what numOfJarsSold[0] is so it could possibly make false positives for certain cases, you can combine the two loops that display the lowest and highest ammount, but it isn't necessary, the code looks a-ok aside from the problem I mentioned earlier.
Well I didn't really read your code thoroughly so I am going to assume you input the ammount in the order the flavors are listed; in which case, you just cout<<salsa[x] when high = numofjarssold[x].
Well the way I would do it is use a for loop to compare the two arrays and a bool array to track what answers are wrong.
for(x = 0; x < 20; x++)
if(correct[x] != answer[x])
wrong[x]=false;
for(x = 0; x < 20; x++) {
if(!wrong[x]) {
cout<<x<<". wrong"<<endl;
total++;
}
else
cout<<x<<". right"<<endl;
}
cout<<total<<"wrong, "<<(20-total)<<"right"<<endl;
Don't forget to initialise the bool array to true, and you can figure out how to do the percentages by your self quite easily. As for fstream, this link is pretty helpful http://www.cplusplus.com/doc/tutorial/files.html
The easiest way to do this is make a for loop and initialize an integer to 0, everytime a number greater than your integer appears, set it to that number; if not, progress with the loop. At the end you should have to largest integer.
int high = 0;
int x;
int salsa[5];
for(x = 0; x < 5; x++)
if(salsa[x] > high)
high = salsa[x];
cout<<high;
finding the lowest value is not much different, but you can easily figure it out from the snippet I gave you. Hope this helps :)
Have you considered using a switch or while loop? After all, you only need to go through each element once right?
Well there are a multitude of ways you can approach this; one being setting a flag for each vowel to disregard it after it has been used(tedious/crude) alternatively you can make a structure of a int and character(probally not what you are looking towards). So what do you do? Well you its simple really, you have already sorted your vowels from highest to lowest, so you dont even need an those encompassing if statements!
for (int z=0; z<5; z++){
if (v[z]==a){
cout<<"There are "<<v[z]<<" instances of the vowel A";
cout<<endl;
continue;
}
if (v[z]==e){
cout<<"There are "<<v[z]<<" instances of the vowel E";
cout<<endl;
continue;
}
if (v[z]==i){
cout<<"There are "<<v[z]<<" instances of the vowel I";
cout<<endl;
continue;
}
if (v[z]==o){
cout<<"There are "<<v[z]<<" instances of the vowel O";
cout<<endl;
continue;
}
if (v[z]==u){
cout<<"There are "<<v[z]<<" instances of the vowel U";
cout<<endl;
continue;
}
}
should suffice(provided you do not have to output a different things for special cases i.e. "there are zero 'A's")
Hmmm, you two are looking far too deeply, there is really no need for a do while at all. You only need three if statements at most(check x, check y, check flag) and four for loops for the array creation portion. But if you are really interested in programming, I suggest looking into a recursive function, rather than resetting count each time. An example would be like
void inc(int x, int y) {
x ++;
if(x < y)
inc(x,y);
}
This example, although crude, gives the basic gist of a recrusive function; it repeatedly calls itself until the parameters are met. If you two are ambitions; try using a recursive function to create your unique grid. Other than that, you two seem to be on the right rack, but remeber to tag && k != j and && j != i to your two if statments respectively. In each of the two cases grid[j] is the exact same thing as grid[k][j] because i is equal to k. I hope this helps, don't give up!
For the purposes of two dimensional arrays, the "rows" and "columns" are declared, in a sense, by the user; depending on which one he or she calls first. For instance, you can do
for(i = 0; i < 9; i++)
for(j = 0; j < 9; j++) {
grid[i][j]=x;
x++;
}
//or
for(j = 0; j < 9; j++)
for(i = 0; i < 9; i++) {
grid[i][j]=0;
x++;
}
both loops will give the same output if called in the same way as they are declared(i first or j first). That being said, he needs to check for both rows and columns for his program
I'm trying to leave some of the code for you to do, but to clarify; you need to check both rows and columns. In this case, you are only checking i. What you need to do is create a second for loop to check j. However here is the problem I am talking about(and was probally vague on) Say j = 5 and you go through your first loop, which checks rows, and find a repetition, j is now 4. After this loop you have a second loop to check columns, and if this also fails, j is now = to 3. So what you need to do is make a flag that prevents the program from entering the second loop if the first check failed.
I remeber doing a similar program a few months ago as a joke with my friend. Our teacher asked us to create a soduku solver so we created an algorithm to test every possible combination of ansewers in a number until it was correct. What you are trying to do(or atleast what I think you are) is create a row of unique numbers while checking the column is unique. However, we used a structure with checksums and arrays for each row/column which might be a little more than you want to get into. But anyways, for your purposes, you are not checking the entire row/column; only the previous element, which makes your problem very simple. Just make two for loops to check the current column and row of the element. In addition, you will want to set the whole array to 0 if you use this method:
for(x = 0; x < 9; x++) {
if(i != x) {
if(grid[i][j] == grid[x][j])
j--;
x = 9;
}
}
just repeat this loop twice for 'j' and it will check the corresponding rows and columns. However, you will also need to make a flag to ignore the second loop if the first one fails so j is not subtracted twice. I hope this helps!