|
| ryujin89 (32) | ||||
| I am probably well on the wrong path with this one which is why I can't get the program to compile in the slightest. The assignment instructions are :
I tried to use the switch statement but not sure if that is correct. Also, it wouldn't allow the return of the flavors in the count function due to them being changed int when applying the update to the count, so I tried to use the atoi() but don't really know how to use it and the tutorials I read about it don't fit my situation close enough for me to manipulate it to make it work. I hope that I have described things enough. Here is my code thus far.
Thanks in advance for any assistance. | ||||
| gcampton (782) | |||
the switch is fine in this case...
| |||
Last edited on | |||
| mcleano (733) | |
Ahh there are quite a few errors. Firstly, when using string objects use the getline() function. So line 21 should be: getline(cin, flavor);Secondly, when comparing a string you should use the pre-defined functions. Therefore line 26 should be: if(!flavor.compare("stop") // notice i have used '!' as this function returns 0 if both strings are the same On the same topic, you have use single quotes for a word; single quotes are only used for single characters. Also, in your assignment brief it says accept flavour names no matter how they are written(capitalization and what not) so I personally would take any input and force all letters to lower case and then do the if statement. Do as you see fit thoughHate to say it but functions aren't your friend, and you need to re-learn them! http://www.cplusplus.com/doc/tutorial/functions/ (particularly function arguements and return values) Post back with ammendments and we'll go from there - if need be :) | |
Last edited on | |
| ryujin89 (32) | |||
| I have changed the code around quite a bit. Is this way of doing things progress? I had to create the shortened names for the flavors because the switch statement was giving error that they were too long. I re-read the tutorial on converting the string to an int and I hope that I was able to understand it. The only error I get is that the compiler says that the "+" operator doesn't do anything when it's adding count to its respective flavor counter.
The instructor assigned this project and set it's due date on the same day that we have a big test, so I'm having to both study and this at the same time as other classes and my job. I'm sorry if I miss something that I shouldn't. Thanks again for the assistance. | |||
| Zhuge (634) | |
| What is the purpose of all the strings named after the Also you can't switch/case on a string; it must be an integral or character value. You'll need to use if/else chains to do that with strings. Also, take the others' advice and accept the input as a string, then convert it to lowercase before comparison. | |
Last edited on | |
| mcleano (733) | |
| I will post a version of this program for you later as I think you will benefit from seeing a working example more than us constantly sending you away | |
| mcleano (733) | |||
Right now this is an example program and does not contain everything required by the brief:
Study this carefully! It is not often I will do this for someone lol! | |||
| ryujin89 (32) | |
| I am very very thankful for your assistance mcleano! The issue I am having right from the start is that if the user enters say chocolate 5 vanilla 10 chocolate 5 mint 7 the output shows chocolate 5 vanilla 10 mint 7 It doesn't add the input if the user enters the same flavor 2+ times. How would I fix this? | |
| mcleano (733) | |||
You would need to add in another integer variable and assign that variable the amount of scoops for that flavour. Then you would just add it to scoop_count[i]:
| |||
| ryujin89 (32) | |||
| Thank you very much mcleano. I have changed around the code to include more of the details from the instructions, but I'm stuck on one last thing. I have the program working other than a formatting setting that I can't figure out how to get to work. I need to only print "DAILY SCOOP REPORT" once at the top of the output, but I've moved it around but due to the way the arrays are set up I don't know where to put it. Here is my code:
Thanks again for all of the assistance. It is greatly appreciated. | |||
Last edited on | |||
| mcleano (733) | |
| Not a problem! I don't mind helping someone who is genuinely in need of it and is willing to put some effort in like you have! It's simple. Just place that cout statement outside of the for loop so it doesn't get outputted on every iteration. When you come across problems like this try and "step-in" to the code, maybe with a pen and paper if need be, and see if you can find a solution tht way. Something you might like to add just after the section of code where you change the first letter, you may want to then go onto to convert every letter thereafter to lowercase because what will happen if I enter "mInT"? Also, you will need to slightly alter that setion because at the moment, "Rocky Road" wouldn't get accepted as valid input. | |
| ryujin89 (32) | |||
| Thank you for all the assistance. I never implied that I wanted anyone to do the work for me and I appreciate your assistance in the areas I'm having problems with. I figured out that why it wasn't working when I moved the "DAILY SCOOP REPORT" line around. I had renamed the file and when I compiled it, it was outputing the "last working configuration" kinda deal if that makes sense. So I created a new project (the .cpp file has to have a certain name for submission) and put the code in it. Now the line is printed only once. In the code block below, I have it where it lowers casing for all other letters other than the first or so it seems to be doing. The reason I have the case coding the way I do is that the instructions want the flavor report to print out with first letter of each word cap and lower after that. I am going to look into how to cap the 2nd "R" in Rocky Road, but other than the ignore white-space I don't really know how. Do I need to parse the line? I tried but it gives error that in the first if statement "syntax error : identifier 'flavor_input'".
| |||
| DrChill (558) | |
Also BTW you put:cout << "valnilla\n"<<endl;Don't know if you caught that yet .... | |
| ryujin89 (32) | |
| Thanks. I typed that part fast w/o thinking. fingers hit wrong key. I was showing my g/f the program as it was and she pointed it out. lol. I prob wouldn't have noticed it if no one had said anything. lol. I'm still working on capitalizing "road" in rocky road. Anyone able to read my last post before this one and assist I would greatly appreciate it. Thanks again everyone for the assistance thus far. | |
| mcleano (733) | |
| Your only synatical problem there is on line 3 & 4 - not exactly correct lol (brackets). I'm assuming line 4 is a nested if statement. Your other problems are with logic. If we take lines 3-5 first, personally I don't like hard-coding value like "flavor_input[6]". I prefer to actually have code that loops through and finds the spaces etc but that is entirely your choice and not really a problem. However, your while loop is lol. It is undoing everything the code I just spoke about does. You increment the iterator with "++it" to skip the first character and loop through the rest of the text to change it to lower case. But what about flavor_input[6]? Isn't that meant to be a capital? Also, check if a letter is lowercase before performing actions on it. Along with toupper() and tolower() there is also isupper() and islower(). Try and come up with a new code block to do this. There are multiple different ways to do this. I've just noticed whilst writing this that the if statments you have don't really make sense. You testing if the iterator is at the end of the string object, but you do no iterator arithmetic to change what its pointing to. | |
| ryujin89 (32) | |||||
| The if statement about "rocky road" and element [6] and such is just something that I tried. I never intended for it to be considered a piece of the code I was confident in. It was only to attempt to get it to work as far as using toupper on the 6th element (the 'r' in "road"). The line means nothing to the actual program other than a failed attempt that I wanted feedback on to see if I was on the right path. After taking out the if statement with "rocky road", I'm not sure what exactly is going on down to the exact detail, but from what I have made sense of the code is that the first statement converts whatever character that is at element [0] to a capital letter. Then the other if statement (not the one about "rocky road") iterates through the other characters and changes characters from element [1] through the end of string to lowercase. If I take out the part about "rocky road", the program runs and works fine. The only issue I'm having is converting the 'r' in "road" to a capital 'R'. I have searched the net and even tho it would eliminate the iterator, it looks as if it would work but I can't get it to merge into my program. The first code block is the original that I copied and pasted from the net. The 2nd block is how I altered it trying to get it to work within my program. 1st block : original copied from website
2nd block : how I altered it to try to make work but it didn't
I am going to continue looking at this as well as other ways to get the output as the instructions require. Even after searching Google with many different phrases and wordings, I haven't found a site that has anything that I am able to understand and/or able to take info from. I take notes in class via typing as he talks (especially getting anything he writes on the boards) and even tho I've never missed a class, I don't have any notes on how to do this type of formatting. I've looked through the book to see if it had it and he just didn't cover it, and still came up with nothing. Any assistance or pointing to somewhere I can get useful information would be greatly appreciated. It's due Tue, but I work every day between now and then and wont have much time to work on it. Thanks for all of your assistance mcleano. I greatly appreciate all that you have done. | |||||
Last edited on | |||||
| mcleano (733) | |||
Whats wrong with the code above?
| |||
| ryujin89 (32) | ||
| it will allow "rocky road" to be input and print out "Rocky Road" as is instructed, but it won't accept any flavor that has random case. ie "chOcoLate" or "roCkY roAd". Whereas the iterator would lower case all letters behind the first. When I typed in the instructions/brief in the very first post, I condensed some lines to stay within the character count limit as well as I thought that "...and so that the user's input will be matched to the correct flavor no matter what type of capitalization is used." would be close enough to show what I needed the code to do. This is the quote from the actual instructions.
Would he not be including random case in that as well? I'm going to try to combine the iterator as well as this code above. | ||
| ryujin89 (32) | |||
| I think I got it to work! I run the program and type in random caps inlcuding space with "rocky road" and the program runs and outputs the way I believe the insturctor wants. Here is how I combined them to make it work:
Do you think that from the quote block on my post previous to this that I have things correct? | |||
| mcleano (733) | |
| From what I can see the program looks completely fine, except one thing! Change the line: if (flavor_input == "STOP" || flavor_input == "Stop") to use the compare function given in the string class, and don't compare this way whether you're using string objects or C-Strings!!! | |
Pages: [1] [2]
This topic is archived - New replies not allowed.
