Hey guys, just checking to see if anyone had a good (not necessarily) program idea. I have just finished a big one, and I have kind of drawn a blank. Any help would be greatly appreciated.
I have an idea about a program , It can be easier for you .Make a program that takes a number and reverse it .It should ask the user to input a number like 97524 and the output should be 42579 .
Ha. I have actually built a program similar to this a while ago, alas I kinda let it go as I knew I wouldn't use it much. Thanks anyway. Any more ideas would be great!
well peronsally I would assign the number to a array and then see how many digits it is and take each of the numbers and place it into another array so each number has a array of its own by using strcpy() (I think or another string function which copys part of a string) and then output the string backwards for example
number 1234, i wud put that into string 1 i wud use a string function to bring 234 into string 2 then 34 into string 3 and 4 into string 4 then
After many a trial and error, I came up with some oddly formatted code. (I write weirdly) that is similar to arcadiu's. (As I used multiple arrays and if and else if statements). The number must be 3 digits and I used int instead of strings.
I agree with firedraco, although it is possible to post solutions that are well beyond the knowledge of the student. Like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
int main() {
std::string num;
std::cout << "Enter a number: " << std::flush;
getline( std::cin, num );
try {
boost::lexical_cast<unsigned>( num );
std::cout << "The number reversed: ";
std::for_each( num.rbegin(), num.rend(), std::cout << boost::lambda::_1 );
std::cout << std::endl << std::endl;
} catch( const boost::bad_lexical_cast& ) {
std::cout << "Input is not a valid number or is out of range" << std::endl;
}
}
Of course there are easier ways to do this; I just tried to use as many different libraries as I could.
Namely, input validation could be done differently, and for_each with boost::lambda could be
done simpler.
Oh, and never mind the subtle bug in the code; it's intentional.
Yah, the point being to post solutions that somebody can't just submit as their homework solution without raising suspicion that the student got the solution from somewhere.
A lot of posters around here post homework problems in the hopes that someone just writes the program for them. We try to help them arrive their own solution rather than spoon feed the solution to them. It does nobody any good to just give out solutions.