![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Web Design & Programming Discussion of web design, and server-side & client-side scripting |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
|
c++ help please -.-
here's the deal
I need to "design my program with a function that takes as an argument an integer between 0-99 and returns a string that contains the integer value in english. It shouldn't have 100 different id else statements, I'm supposed to use % and / to "extract the tens and ones digits to construct the english string" all of this drama is to make a program that outputs the lyrics from "Ninety-nine bottles of beer on the wall" I'm not looking for someone to write the code for me (although I wouldn't say no hah) what I am looking for though is someone who could mayhaps explain to me *** all that nonsense means I have absolutely no idea where to start...I barely understand what a function is in fact, I don't understand programming at all hah what I have so far is #include "stdafx.h" #include <iostream> #include <string> using namespace std; int beer (int number_par); { } int main (void) { } so basically...nothing. Any help would be appreciated cause like I said I don't even know where to THINK about beginning how to approach this -.- thanks
__________________
"Health and long life are the gifts of the spice!" |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Design Team Member
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,880
OS: Vista, various linux distros
|
Re: c++ help please -.-
Hey, this isn't too hard of a problem. You have the basic frame set out... though if beer() is the function you're talking about it should have a string datatype:
Code:
string beer(int number_par) You have all the information you need to approach the problem... Do you know the %(modulus) operator? if not then basically 25/10 = 2.5(the division)(which could then be modulated by 1 to make .5 which could then be subtracted from the result) whilst 25%10 = 5 (the remainder) Using that knowledge you should be able to parse the digits from the number then you could use a switch-case statement to work out what that number is as a string Cheers, Jamey |
|
|
|
|
|
#3 (permalink) |
|
Registered User
|
Re: c++ help please -.-
yeah beer was the name of the function...it's little things like that that I miss and don't even think about taht it should be a string and not an int...ack I hate this stuff hah
that...modulus thing dind't make sense to me at all -_-...I don't understand what you mean by 2.5 could be modulated to 1 to make .5 or what parsing digits is or what a switch case statement is hah but much thanks for the link, I'm defintiely gonna go check that out
__________________
"Health and long life are the gifts of the spice!" |
|
|
|
|
|
#4 (permalink) |
|
Moderator: Design
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,454
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 BETA
|
Re: c++ help please -.-
Modulus is just like division, except the remainder is stored in the variable address instead of the quotient. The rest of Jamey's post was gibberish to me as well.
There should be a way to loop this around in mid-cout without causing a catastrophic logical error.
__________________
![]() Validate your Markup Validate your CSS Notepad++ Please use [html], [php], and [code] when posting code or markup. I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message. |
|
|
|
|
|
#5 (permalink) |
|
Design Team Member
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,880
OS: Vista, various linux distros
|
Re: c++ help please -.-
Haha, sorry my last post was a lot of gibberish, it was late, forgive me.
This would be the basic algorithim behind converting a number into a string using %, / and a smaller switch case statement: (example input = 189) While the number is > 0: - Modulus the number by 10 (would return 9) - Use switch case between 0 and 9 to add "0"-"9" to the string - Divide the number by 10 (would return 18) - Modulus the number by 10 (would return 8) - Use switch case again - Divide the number by 10(would return 1) - Modulus the number by 10 (would return 1) - Use switch case again - Divide the number by 10(would return 0 exiting the while loop) Sorry, it's hard to know whether this is clear or not, but i can't explain this using C++ right now because for the life of me i can't remember enough C++ to do it... I expect it would be something like(though i haven't tested this): Code:
//...other code here...
int numberToWorkOn = number_par;//the number to work on(as a parameter)
String myString = "";//an empty string
//while the number is bigger than 0
while(numberToWorkOn>0){
//modulus the number by 10
int tempNo = numberToWorkOn % 10;
//work out what the digit is
switch(tempNo){
case 0:
myString = "0"+myString;
break;
case 1:
myString = "1"+myString;
break;
//and so on until 9
}
//divide the number by 10 and loop around
numberToWorkOn = numberToWorkOn/10;
}
Cheers, Jamey |
|
|
|
|
|
#6 (permalink) |
|
Registered User
|
Re: c++ help please -.-
I actually had a friend somewhat explain that to me, something about using like say 99 was the first number...would do 99/10 as an int...would become 9, which would count as the 10's place number, and then 99 % 10 as an int to get 9, which would be the 1's place number...all this is so over my head, I'm sure you're explaining it perfectly clearly but when I see c++ my eyes just wanna close! lol
But I appreciate your help, at least now I have a visual model I can see and that really will help me out a ton, I really appreciate it, after I study it for a few hours I should get it lol
__________________
"Health and long life are the gifts of the spice!" |
|
|
|
|
|
#7 (permalink) |
|
Design Team Member
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,880
OS: Vista, various linux distros
|
Re: c++ help please -.-
lol haha, trust me i wasn't explaining it perfectly clearly... but if you understand what you're friend has said then you're okay =]
Hope all goes well. Cheers, Jamey |
|
|
|
![]() |
| Thread Tools | |
|
|