0

First off, this is for a homework assignment, so I'd appreciate help and guidance rather than just the answer in code.

The purpose of the code should be for a user to input a number and a width.

If the width is longer than the number, the number will be printed out with zeros in front of the number. For example 43 3 would give 043.

If the width isn't longer just the number would be printed: 433 2 would be 433.

I think I have to get the count of characters in the number and compare it to the count of characters in the width (if-else statement).

Then, if the number of characters in the number is more, print out the number. Else, print out the width.

I think I get the number of zeros by subtracting the length of the number from the length of the width. Then use that to set the number of zeros. Like I said this is homework and would rather learn than be given the answer.

If anyone can help, it'll be appreciated.

    #include <iostream>;
    #include <string>;

    using namespace std;

    string format(int number, int width) {


    int count = 0;
      if (number > width)// This if-else is incomplete
          return ;  
      else              

    }

    int main() 
    {
     cout << "Enter a number: ";
     string n;
     cin >> n;

     cout << "Enter the number's width: ";
     string w;
     cin >> w;

     format(n, w);

    }
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Joe Smith
  • 1
  • 1
  • 3
    What is your question? – yizzlez Oct 25 '15 at 00:35
  • use `atoi()` to convert `n.c_str()` to an `int` – Ryan Oct 25 '15 at 00:37
  • Sorry, really frustrated and forgot the questions. Is my thinking right and how do you get the ints to work into the string function. – Joe Smith Oct 25 '15 at 00:37
  • The *code* is formatted okay, it's just the morass of English text that's hard to read :-) Let's see what we can do about that ... – paxdiablo Oct 25 '15 at 00:39
  • We haven't learned about atoi yet. I saw that in my GOOGLE journey, though. The professor wants use to use what we learned so far(basic beginner stuff) like size() and length(). – Joe Smith Oct 25 '15 at 00:40
  • @self `atoi` is rarely a good solution. – Derek Oct 25 '15 at 00:46
  • 1
    Then I'd suggest reading the numbers into ints instead of strings. – Yay295 Oct 25 '15 at 00:50
  • @Derek lol. use `strtod` then, -- or roll your own which is a horrible idea. This is C land, these are functions available from the standard library – Ryan Oct 25 '15 at 01:09
  • @self How would we use `strtod` with integers? I'd suggest using `stringstream` instead of `atoi`. The question is tagged as C++, so this is not "C land". – Derek Oct 25 '15 at 01:12

1 Answers1

0

no need to checking string or other things write these code C++ will do it for you automatically.

#include <conio.h>
#include <iostream>
using std::cout;
using std::cin;

#include <string>;
using std::string;

#include <iomanip>
using std::setw;

void format(int number, int width)
{
    cout.fill('0');

    cout << setw(width) << number;
}

int main()
{
    cout << "Enter a number: ";
    int n;
    cin >> n;

    cout << "Enter the number's width: ";
    int w;
    cin >> w;

    format(n, w);

    _getch();
    return 0;
}
  • 2
    You aren't using strings, so why `#include `? and what happens when I enter text instead of a number? Also, `conio.h` is not a standard C++ header. – Yay295 Oct 25 '15 at 00:55
  • excuse me i just copied code and edited it, and what you mean I entered and string when you talking about number and width it must be an integer. –  Oct 25 '15 at 01:00
  • Thank you first off, so you're filling in the width with zeros where there's no digits? That seems so much simpler. – Joe Smith Oct 25 '15 at 01:02
  • and by the way I used conio.h just because of _getch(), but you can press ctrl + F5 instead of using _getch(). –  Oct 25 '15 at 01:03
  • You could also use `std::cin.ignore();` and not use `conio.h`. – Yay295 Oct 25 '15 at 01:03
  • yes it fills 0 instead of spaces and then set's the width with setw(width). –  Oct 25 '15 at 01:07