> HELP!!! with writing this program on C++ about strings?

HELP!!! with writing this program on C++ about strings?

Posted at: 2014-12-18 
I believe Ratchetr is correct, that this is not a program about strings. Here's an example of what I think you should do:

#include

#include

#include

?

using namespace std;

?

unsigned numDigits(unsigned);

unsigned msd(unsigned);

unsigned lsd(unsigned);

int main(int argc, char *argv[]) {

? ? int n;

? ? string in;

? ? stringstream ss;

? ? while (true) {

? ? ? ? do {

? ? ? ? ? ? cout << endl << "Enter an integer: ";

? ? ? ? ? ? getline(cin, in);

? ? ? ? ? ? ss.clear(); ss.str(in);

? ? ? ? } while (!(ss >> n));

? ? ? ? cout << numDigits(abs(n)) << " digits in " << n << endl;

? ? ? ? cout << "most significant digit: " << msd(abs(n)) << endl;

? ? ? ? cout << "least significant digit: " << lsd(abs(n)) << endl;

? ? }

? ? return 0;

}

// Return count of digits in n

unsigned numDigits(unsigned n) {

? ? if (n < 10) return 1;

? ? return 1 + numDigits(n / 10);

}

// Return most siginificant digit of n

unsigned msd(unsigned n) {

? ? while (n > 9) { n /= 10; }

? ? return n;

}

// Return least significant digit of n

unsigned lsd(unsigned n) {

? ? return n % 10;

}

#if 0

Sample run:

Enter an integer: 7392

4 digits in 7392

most significant digit: 7

least significant digit: 2

#endif

My guess is they want you to use a numeric variable type, not a string. Most likely you would use int or long. What if I enter -35x. Is the first digit of that number a -? Is the last digit an x?

Div and Mod ( / and % ) are what you will need to use here.

#include

#include

using namespace std;

//Function

string first_digit(string number)

{

return number.substr(0,1);

}

int length_digit(string number)

{

return number.length();

}

string last_digit(string number)

{

return number.substr(number.length() - 1, 1);

}

//Main follows

int main()

{

string number;



cout << "Please insert a number: ";

cin >> number;

cout << "The first digit of your sequence is: " << first_digit (number) << endl;

cout << "The last digit of your sequence is : " << last_digit (number) << endl;

cout << "The length of your sequence of numbers is: " << length_digit (number) <
return 0;

}

You have MANY problems, but all are similar.

cout << "The first digit of your sequence is: " << first_digit (number) << endl;

string first_digit(string number)

{

return (number.substr(0,1));

}

int length_digit(string number)

{

return (number.length());

}

There are so many ways to do this. I want 78 dollars to write this program for you.

I have to write a program for class in PuTTy or Dev-C++ and the program, when given a number, for example 7392, must output the first number, last number and the number of digits.

So, for 7392, it will output first number as 7, last number as 2, and number of digits as 4.

So far this is my program.

//Program will take a given set of numbers and return the first digit, last digit, and the number of digits of the the argument

#include

#include

using namespace std;

//Function

int first_digit(string number)

{

first_digit=number.substr(0,1);

}

int length_digit(string number)

{

int l=0;

length_digit=l;

l= number.length();

}

int last_digit(string number)

{

last_digit=number.substr(l, 1);

}



//Main follows

int main()

{

cout << "Please insert a number: " ;

string number;

cin >> number >> endl;

cout << "The first digit of your sequence is: " << first_digit (int number) << endl;

cout << "The last digit of your sequence is : " << last_digit (int number) << endl;

cout << "The length of your sequence of numbers is: " << lenght_digit (int number) <


return 0;

}