Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 51
  1. #1
    Yoshi P
    Join Date
    Nov 2006
    Posts
    5,074
    BG Level
    8
    FFXI Server
    Quetzalcoatl
    WoW Realm
    Proudmoore

    Need a little help with a c++ project

    Hello everyone;

    I need to get this project done by tomorrow, first time I am taking a c++ class and I've been a little slacky with it. For some reason Visual Studio crashes everytime I try to install it so...can't try stuff either :/

    here is the project:

    You will write a program that will help an elementary student learn how to expand positive, integer
    values. Your program will ask the student to enter a number between 1000 & 9999 inclusive. Your program
    will print this number in expanded form.

    Input: One positive, 4-digit, integer value. Be sure to give an appropriate message if the input is invalid.

    Output: The expanded form of this integer.

    Processing: In order to get from the input to the output, you will need to make use of the arithmetic operators
    we have used in class (specifically the % and / operators for the integer).

    Ease of Use: The interaction between the program and the user should be clean and easy to read!


    And here is how it should like:
    http://i4.photobucket.com/albums/y12...reira/proj.png

    I've asked on couple of c++ boards and they pretty much told me to use "modulus by 10" method. Don't know how I'll apply that to this and posters there were too "pro" to get into the detail.

    Thanks in advance.

  2. #2
    Salvage Bans
    Join Date
    Dec 2005
    Posts
    946
    BG Level
    5

    Re: Need a little help with a c++ project

    % = modulus

    5653 % 10 = 3
    565 % 10 = 5
    56 % 10 = 6
    5 % 10 = 5

    Hope that helps!

  3. #3
    Black Belt
    Join Date
    Oct 2006
    Posts
    5,799
    BG Level
    8

    Re: Need a little help with a c++ project

    What exactly are you having problems with? The algorithm seems incredibly simple. I don't have an c++ experience, but from my hazy memory of a java intro class 4 years ago, it sounds like all you have to do is check the number of characters in a string and output either an error message or "c1 * 1000 + c2 * 100 + c3 * 10 + c4 * 1". You should be familiar with the syntax for if/thens, so I'd guess you need to look up how to read a string length and print specific characters in a string.

  4. #4
    Hydra
    Join Date
    Dec 2007
    Posts
    127
    BG Level
    3
    FFXI Server
    Carbuncle

    Re: Need a little help with a c++ project

    Yes, use modulus. I would think you learned it as this point if you have an assignment which uses it.
    http://www.cprogramming.com/tutorial/modulus.html

  5. #5
    Yoshi P
    Join Date
    Nov 2006
    Posts
    5,074
    BG Level
    8
    FFXI Server
    Quetzalcoatl
    WoW Realm
    Proudmoore

    Re: Need a little help with a c++ project

    Thanks =)

  6. #6
    Sea Torques
    Join Date
    Aug 2007
    Posts
    629
    BG Level
    5

    Re: Need a little help with a c++ project

    Quote Originally Posted by Beckwin
    What exactly are you having problems with? The algorithm seems incredibly simple. I don't have an c++ experience, but from my hazy memory of a java intro class 4 years ago, it sounds like all you have to do is check the number of characters in a string and output either an error message or "c1 * 1000 + c2 * 100 + c3 * 10 + c4 * 1". You should be familiar with the syntax for if/thens, so I'd guess you need to look up how to read a string length and print specific characters in a string.
    lolwat? if((input < 1000) || (input > 9999)).

  7. #7
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    Re: Need a little help with a c++ project

    Well first of all, the input method will probably parse it as an int and not a string. Rather than try to check the "number of characters" with comparitors, it's easier to work from the right side, then use string concatenation to put it together along the way. So for some semihelpful pseudocode:

    int input = StrInputFunction();
    int in = input;
    String out = "";
    while (in != 0)
    {
    out = strCat(" * 1", out);
    out = strCat(in%10, out);
    in = in/10;
    }
    strOutput ("the expanded equivalent of %i is:\n%s", input, out);

    and repeat the last 4 lines. Note that I never actually learned C++, so that's all just a halfassed hybrid of java and C, and I don't remember the names of syntax for keyboard input, string concatenation or string output, so you'll have to look those up, and then worry about formatting the output (you could use some sort of iteration to decide how many 0s to stick in, or just take the slopy lazy way out and hardcode it up to like 7 digits or whatever). The important algorithm is: find in%10, stick that to the front of the string, divide in by 10, repeat until in is 0.

  8. #8
    New Merits
    Join Date
    Oct 2005
    Posts
    204
    BG Level
    4

    Re: Need a little help with a c++ project

    Quote Originally Posted by Charla
    Well first of all, the input method will probably parse it as an int and not a string. Rather than try to check the "number of characters" with comparitors, it's easier to work from the right side, then use string concatenation to put it together along the way. So for some semihelpful pseudocode:

    int input = StrInputFunction();
    int in = input;
    String out = "";
    while (in != 0)
    {
    out = strCat(" * 1", out);
    out = strCat(in%10, out);
    in = in/10;
    }
    strOutput ("the expanded equivalent of %i is:\n%s", input, out);

    and repeat the last 4 lines. Note that I never actually learned C++, so that's all just a halfassed hybrid of java and C, and I don't remember the names of syntax for keyboard input, string concatenation or string output, so you'll have to look those up, and then worry about formatting the output (you could use some sort of iteration to decide how many 0s to stick in, or just take the slopy lazy way out and hardcode it up to like 7 digits or whatever). The important algorithm is: find in%10, stick that to the front of the string, divide in by 10, repeat until in is 0.
    Chances are they're using the iostream header since it's a formal (and seems like a beginner) class. The professor is probably expecting a lot of hardcoding too then, so the most probable method for the program that I can see is to use simple division since the numbers will always round down.

    This is probably the simplest way to do the program (I realize it is no way the most useful method and doesn't make use of the % operator like the professor may want).
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int num = 0;
       cout << "Enter a number: ";
       cin >> num;
    
       //Put validation code here for < 1000 and > 9999
    
       cout << "\n***************************************************\n";
       cout << "The expanded equivalent of " << num << " is:\n\n";
       for (int i = 1000; i >= 1; i /= 10)
       {
          cout << num / i << " * " << i;
          if (i > 1) cout << "  +  ";
          num -= num / i * i;
       }
       cout << "\n***************************************************\n";
    
       return 0;
    }

  9. #9
    Relic Horn
    Join Date
    Dec 2005
    Posts
    3,363
    BG Level
    7
    FFXIV Character
    Xanthe Celaeno
    FFXIV Server
    Hyperion
    FFXI Server
    Carbuncle

    Re: Need a little help with a c++ project

    No need to break up the input string.

    Code:
    cin >> input;
    
    const int min = 1000;
    const int max = 9999;
    
    String output = "";
    int value = input;
    
    if(value >= min && value <= max) {
      for(int i = 1; value > 0; i *= 10) { // Use i to keep track of current column
        if(i == 1) {
          // Don't append for first column
          output = (value % 10) + " * " + i;
        }
        else {
          // Append previous output for all remaining columns
          output = (value % 10) + " * " + i + " + " + output;
        }
        value /= 10; // Progressively chop off each column, should ignore remainder since it's integer division
      }
    
      cout << output << endl;
    }
    else {
      cout << value << " is outside of required value range " << min << " <-------> " << max << endl;
    }
    I like this implementation since it scales very well should your boundary values ever change. Not something that's important given the scope of this project, but in a real-world situation it would be ideal. Pardon any syntax errors; I've been working mostly in Java these days and it's been a while since I've done any C++ work. You'll need to pretty it up as well, I can't be doing all the work. :wink:

    [edit]
    Danit it, Haelian beat me to the simpler approach. I think I still like how mine is done better.

  10. #10
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    Re: Need a little help with a c++ project

    First of all, they might not have reached that complexity of order of operations stuff yet. Also, I see % as much simpler than screwing around with x/10*10 (which might work differently on some compilers or something IIRC).

  11. #11
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    Re: Need a little help with a c++ project

    Don't copy someone else's code, just use the information you've gotten and write it yourself. You'll only fuck yourself over in the long run if you take someone else's code and pass it off as your own without learning the concepts yourself.

    You know how modulus works now so the assignment should be fucking easy now. Just mod by different factors of ten and you're set. Obviously ouput as you mod, or save the values for print later. Very simple.

  12. #12
    Relic Horn
    Join Date
    Dec 2005
    Posts
    3,363
    BG Level
    7
    FFXIV Character
    Xanthe Celaeno
    FFXIV Server
    Hyperion
    FFXI Server
    Carbuncle

    Re: Need a little help with a c++ project

    Quote Originally Posted by Charla
    First of all, they might not have reached that complexity of order of operations stuff yet. Also, I see % as much simpler than screwing around with x/10*10 (which might work differently on some compilers or something IIRC).
    I agree, % is definitely the way to go, especially considering the project description:
    Processing: In order to get from the input to the output, you will need to make use of the arithmetic operators
    we have used in class (specifically the % and / operators for the integer)
    .
    Aside from some creative use of the for loop, I think everything I've done should be in the scope of a beginner's C++ class. I realized the complexity comment was likely directed at Haelian, but I just wanted to be clear.

  13. #13
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    Re: Need a little help with a c++ project

    Quote Originally Posted by Maguspk
    Don't copy someone else's code, just use the information you've gotten and write it yourself. You'll only fuck yourself over in the long run if you take someone else's code and pass it off as your own without learning the concepts yourself.

    You know how modulus works now so the assignment should be fucking easy now. Just mod by different factors of ten and you're set. Obviously ouput as you mod, or save the values for print later. Very simple.
    That's why I only wrote pseudocode. That and I never actually learned C++, and wasn't even sure if + could be used for string concatenation.

  14. #14
    New Merits
    Join Date
    Oct 2005
    Posts
    204
    BG Level
    4

    Re: Need a little help with a c++ project

    Quote Originally Posted by Xanthe
    [edit]
    Danit it, Haelian beat me to the simpler approach. I think I still like how mine is done better.
    Ya, yours would hold up better if they wanted to change the size of the min and max. But I say there's no need for extra work!
    Quote Originally Posted by Charla
    First of all, they might not have reached that complexity of order of operations stuff yet. Also, I see % as much simpler than screwing around with x/10*10 (which might work differently on some compilers or something IIRC).
    If they've read up on modulus there's no way they don't know the order of operations. That's first or second day stuff I'm pretty sure. As for the num -= num / i * i; well, it doesn't read very well but it should absolutely work no matter the compiler. Compilers wouldn't change the fact that C++ will round integers and go by the order of operations.

    Anywho, modulus would make it look a little neater I admit but I think my way's cool (screw The Man, program the way you want to).

  15. #15
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    Re: Need a little help with a c++ project

    Quote Originally Posted by Charla
    Quote Originally Posted by Maguspk
    Don't copy someone else's code, just use the information you've gotten and write it yourself. You'll only fuck yourself over in the long run if you take someone else's code and pass it off as your own without learning the concepts yourself.

    You know how modulus works now so the assignment should be fucking easy now. Just mod by different factors of ten and you're set. Obviously ouput as you mod, or save the values for print later. Very simple.
    That's why I only wrote pseudocode. That and I never actually learned C++, and wasn't even sure if + could be used for string concatenation.
    It can't unless you define it to do that, I believe; my post wasn't directed at anyone in particular just advice to the OP not to be tempted to copy code and pass it off as his own. Besides, it's much more gratifying when you write code and it works after you fix errors and stuff. Probably one of the most unique feelings in the world, really good sense of accomplishment.

    Quote Originally Posted by Haelian
    Quote Originally Posted by Xanthe
    [edit]
    Danit it, Haelian beat me to the simpler approach. I think I still like how mine is done better.
    Ya, yours would hold up better if they wanted to change the size of the min and max. But I say there's no need for extra work!
    Quote Originally Posted by Charla
    First of all, they might not have reached that complexity of order of operations stuff yet. Also, I see % as much simpler than screwing around with x/10*10 (which might work differently on some compilers or something IIRC).
    If they've read up on modulus there's no way they don't know the order of operations. That's first or second day stuff I'm pretty sure. As for the num -= num / i * i; well, it doesn't read very well but it should absolutely work no matter the compiler. Compilers wouldn't change the fact that C++ will round integers and go by the order of operations.

    Anywho, modulus would make it look a little neater I admit but I think my way's cool (screw The Man, program the way you want to).
    Yes, screw the man, program inefficiently if you want to.

  16. #16
    E. Body
    Join Date
    Jun 2006
    Posts
    2,181
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    Re: Need a little help with a c++ project


  17. #17
    New Merits
    Join Date
    Oct 2005
    Posts
    204
    BG Level
    4

    Re: Need a little help with a c++ project

    Quote Originally Posted by Maguspk
    Quote Originally Posted by Haelian
    Quote Originally Posted by Xanthe
    [edit]
    Danit it, Haelian beat me to the simpler approach. I think I still like how mine is done better.
    Ya, yours would hold up better if they wanted to change the size of the min and max. But I say there's no need for extra work!
    Quote Originally Posted by Charla
    First of all, they might not have reached that complexity of order of operations stuff yet. Also, I see % as much simpler than screwing around with x/10*10 (which might work differently on some compilers or something IIRC).
    If they've read up on modulus there's no way they don't know the order of operations. That's first or second day stuff I'm pretty sure. As for the num -= num / i * i; well, it doesn't read very well but it should absolutely work no matter the compiler. Compilers wouldn't change the fact that C++ will round integers and go by the order of operations.

    Anywho, modulus would make it look a little neater I admit but I think my way's cool (screw The Man, program the way you want to).
    Yes, screw the man, program inefficiently if you want to.
    Oh come on, there's nothing inefficient about my method. And you have to keep in mind that this is a simple class project, there's no need to put extra effort into something that the professor expects to be simple. I was just sort of looking for my own way to do the program without using modulus, it's fun to find ways around specific methods.

    When I first started learning about what programming was and my first programming language (byond.com, back in probably 9th grade), I found looking at other people's completed code helped a lot more than any text book. Of course, completely cutting chunks out and pasting them onto your on projects will lead to problems but if you take the time to look and understand what the programmer was doing you'll learn a lot.

  18. #18
    Relic Horn
    Join Date
    Dec 2005
    Posts
    3,363
    BG Level
    7
    FFXIV Character
    Xanthe Celaeno
    FFXIV Server
    Hyperion
    FFXI Server
    Carbuncle

    Re: Need a little help with a c++ project

    I was always a fan of taking extra steps for an airtight program. Professors love that kind of effort. Doing the extra work might not influence your grade, but it definitely builds up your reputation (big deal when you start going around requesting recommendations for jobs/grad school).

  19. #19
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    Re: Need a little help with a c++ project

    Quote Originally Posted by Xanthe
    I was always a fan of taking extra steps for an airtight program. Professors love that kind of effort. Doing the extra work might not influence your grade, but it definitely builds up your reputation (big deal when you start going around requesting recommendations for jobs/grad school).
    Yep. Also, it's good practice to find optimal solutions because that's what programming is for. Too many people learn programming inefficiently and don't analyze their code and it's order, etc. Like people who use Bubble Sort in every case of sorting just because it's easy, when if their case were to ever get larger or very unordered it'd be extremely inefficient and they'd be wasting CPU. Nowadays we are spoiled so it's understandable to fool around and not try to write code the best it can be, but doing so really separates a skilled programmer from a mediocre one.

    @Haelian, while looking at other people's code is great, I won't argue with you there, you missed the point of my post. I was just trying to make sure he understood copying code was bad, and understanding the concepts to write the code himself is the way to go when learning to program.

  20. #20
    Yoshi P
    Join Date
    Nov 2006
    Posts
    5,074
    BG Level
    8
    FFXI Server
    Quetzalcoatl
    WoW Realm
    Proudmoore

    Re: Need a little help with a c++ project

    Excellent, thanks alot for the help everyone. Codes were very helpful for understanding the concept.
    I definitely have no intention of copy/pasting codes entire semester, just missed a bunch of classes and now working on catching up with the material.

Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. BG PHP Programmers - Need a little help
    By Galaika in forum General Discussion
    Replies: 13
    Last Post: 2012-05-02, 17:12
  2. I need BG's help with ideas for an advertising campaign!
    By Venga in forum General Discussion
    Replies: 16
    Last Post: 2009-04-09, 07:21
  3. Help with Java Programming Project. Get GF off my back plz
    By Azkarin in forum General Discussion
    Replies: 18
    Last Post: 2007-10-10, 10:59
  4. Need more help with my project
    By Amarao in forum General Discussion
    Replies: 17
    Last Post: 2005-09-26, 10:05
  5. A little help with dreamweaver mx 2004...
    By DarkMistSkeleton in forum General Discussion
    Replies: 2
    Last Post: 2005-08-22, 22:30