Jump to content

Need A Program Challenge


Archerzz

Recommended Posts

I just started to learn how to program in C++ and I need an idea for a program that will challenge me but not be out of my league

 

Could someone give me a good one that if I have a problem (which I most likley will) could help me figure it out?

Share this post


Link to post
Share on other sites

  • Replies 60
  • Created
  • Last Reply

Top Posters In This Topic

Heh... I would recommend learning a good overview of C or C++.

Use a pointer to pass an integer from a character array to a function to multiply that times a randomly selected number.

507484[/snapback]

 

Bet KB can't do that? :rolleyes: KB should try too!!

Share this post


Link to post
Share on other sites

Well, he didn't take up my challenge, and he said he just started. I'm guessing he's around the Array area... possibly.

 

But yesh, what is your skill level? We must know...

 

 

 

And markie, I CAME CLOSE!

 

#include <iostream>
#include <stdlib.h>

int rand()
/* from K&R
  - produces a random number between 0 and 32767.*/
{
   rand_seed = rand_seed * 1103515245 +12345;
   return (unsigned int)(rand_seed / 65536) % 32768;
}
// [/copyandpaste] Why? Because I couldn't figure out your definition =P
using namespace std;

int add(int number)
{
    int newnumber;
    int randomNumber=rand();
    newnumber = newnumber + randomNumber;
    
    cout << "The number is: ";
    
    system("pause");
    
}

int main(void)

{
    int[] numbarArray;
    numbarArray[1] = 1;
    int* thePointar = &numbarArray[1];
    
    add(thePointar);
    
    return 0;
    
}

 

 

Yea... close.

Share this post


Link to post
Share on other sites

lol oh, you wanted an actual random number function then? How come you're not using the "built in" rand() function?

 

What I gave you was the manual page on how to use the standard library rand()

 

Anyhow...

 

You appear to have the int array backwards.

 

int[] numbarArray; should be more like int numbarArray[1], which is actually a little pointless for just one number lol... and numbarArray[1] might want to start from 0 instead of 1, unless you increase numbarArray to [2] to hold two numbers numbarArray[2] wouldn't make spaces [1] and [2], it makes [0] and [1] ;)

 

You're trying to pass a pointer to an integer, so add() should be declared add(int *number);

 

and uhm..... add() doesn't seem to do anything with the pointer to the integer you're passing in yet :) But to get the value inside add() now you'll want to say *number, not just number...

 

EDIT: not too bad though. Just silly little mistakes there really :) my skill level? Not much :P If I was skilled I guess I would be making actual proper graphical applications and stuff, not still stuck on console stuff :(

Share this post


Link to post
Share on other sites

Because I don't read man pages very well ;).

 

 

And don't worry, console work is the core of any program. All that fancy shamshy OpenGL and windows.h crap is a shell over the actual work.

 

 

 

But, Hacker, what's your skill level? Give us an example of what you've learned... (if I see int* psz[...], I'll slap you for using C++ for Dummies).

Share this post


Link to post
Share on other sites

I shall tell you my league but you will laugh because I just started learning about 5 hours ago.... so I guess I am a padawan :D

 

I am trying to make a program that will ask you for a start number then a stop number then the number you wnat to count by and it will count from the start to the stop so if start was 2 and stop was 10 and count by was 2 it would be like

2, 4, 6, 8

but I cant figure out how to declare the strings in the array or thats what the error is saying.

can anyone tell me what is wrong?

 

#include <iostream>
#include <string>
using namespace std;

int countBy()
{
   string myArray[3] = (start, stop, count);
   cout << "Where do you want to start? ";
   cin >> start;
   
   cout << "Where do you want to stop? ";
   cin >> stop;
   
   cout << "What do you want to count by? ";
   cin >> count;
   
   while (start < stop)
   {
         cout << start * count;
   }
   return 0:
}

 

i fixed some of the small errors like the : at the end

Share this post


Link to post
Share on other sites

Well, you don't really want to be putting numbers in to a string - even if you had got the syntax right - anyway, not when "cin" can read number in to an int for you. So, first off replace:

 

string myArray[3] = (start, stop, count);

 

with

 

int start, stop, count

 

Then the loop, well start is always going to be smaller than stop - unless the person entering the numbers happens to put start bigger than stop - so that's an infinite loop :) You'd probably be better off with a for loop something along the lines of..

 

for (i = start; i <= stop; i += count)

 

Then inside the loop, multiplying start by count is always going to end up with the same number, depending on what the user entered for start and count. If you use the for loop above you could do:

 

cout << i << endl;

 

Inside the loop.

 

Then you're missing main(), so either change int countBy() to int main(), or add in a main() and call countBy() from it.

 

 

 

 

 

 

 

 

The finished product... (which I am not entirely sure I should post, so stop right here if you dont want to see it?) :)!

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream>
#include <string>

int countBy(void);

using namespace std;

int main(void) {
countBy();

system("pause");

return 0;
}

int countBy()
{
  int start, stop, count, i;

  cout << "Where do you want to start? ";
  cin >> start;

  do {
      cout << "Where do you want to stop? ";
      cin >> stop;
  } while (stop < start);

  cout << "What do you want to count by? ";
  cin >> count;

  for (i = start; i <= stop; i += count)
  {
        cout << i << endl;
  }
  return 0;
}

 

You'll notice I have a loop around where it asks where they want to stop. That loop just checks if start is smaller than stop, because the for loop won't work backwards. If it is, it'll ask for the stopping number again until it's bigger than start. You could have done some sort of if around the for loop and reversed the loop in that kind of condition instead I suppose. Or maybe figured out a way to do the loop regardless of whether start was smaller than stop :)

 

This is my 4000th post!!!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×
×
  • Create New...