Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Begginner C++ Help  (Read 903 times)

0 Members and 1 Guest are viewing this topic.

Xiphirx

wat
Begginner C++ Help
« on: September 12, 2009, 07:01:09 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
So here's my problem:

Quote
Write a program to input a long integer then print it with commas if it has more than three digits. For example, -2036 and 1234567890 would be printed as -2,036 and 1,234,567,890, respectively.

Here's what I came up with:

Code: [Select]
long input;
int numberoften;
cout << "Enter a number ";
cin >> input;
cout << endl;

if (input > 1000000000 || input < 1000000000)
{
          cout << input/1000000000 << ",";
}
if (input > 1000000 || input < 1000000)
{
          cout << (input/1000000)%1000 << ",";
}
if (input > 1000 || input < 1000)
{
          cout << (input/1000)%1000<<",";
          cout << input%1000;
}


cout << endl;

So, my code works perfectly for positive numbers, but it fails for negative numbers. I am guessing something is wrong with my ||'s or the data type? (lost)

Also, if I put something like 1024 into the program it outputs 1,24 ...

How am I supposed to fix the program only using basic math functions, cout, and if?

Logged
  • For The Swarm
Re: Begginner C++ Help
« Reply #1 on: September 12, 2009, 07:18:58 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
Code: [Select]
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    long int nInput = 0;
    std::cout << "Enter a number: ";
    std::cin >> nInput;
    std::cout << std::endl;

    std::string strVal;
    std::stringstream strStream;
    strStream << nInput;
    strStream >> strVal;

    signed short int counter = 1;
    size_t len = strVal.length();
    signed short int i = strVal.length()-1;
    signed short int end = 0;

    if ( strVal[0] == '-' )
    {
        end = 1;
        --len;
    }
    for ( ; i >= end; --i, counter++ )
    {
        if ( counter % 3 == 0 && counter != len )
            strVal.insert(i, ",");
    }
    std::cout << strVal << std::endl;
}

Only allowed cout, cin and such? 'cause this seems easier XD
« Last Edit: September 12, 2009, 07:37:47 pm by TheDarkJay »
Logged

Xiphirx

wat
Re: Begginner C++ Help
« Reply #2 on: September 12, 2009, 07:19:51 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Nope, just cout, if and math functions ...
Logged
  • For The Swarm
Re: Begginner C++ Help
« Reply #3 on: September 12, 2009, 07:23:19 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
You forgot the negatives signs on each number ;P
Logged

Xiphirx

wat
Re: Begginner C++ Help
« Reply #4 on: September 12, 2009, 07:36:02 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Ok, well that was me being stupid.

Anyway, I fixed it and it now works (yay me)

Code: [Select]
long input;
int numberoften, abs, nxt;
abs = 1;
nxt = 1;
cout << "Enter a number ";
cin >> input;
cout << endl;

if (input > 1000000000 || input < -1000000000)
{
          cout << input/1000000000 << ",";
          if (input < -1000000000)
          {
           abs = -1;
          }         
          nxt++;
}
if (input > 1000000 || input < -1000000)
{
          if (abs*((input/1000000)%1000) < 100 && nxt > 1)
          {
           cout << "0" << abs*((input/1000000)%1000) << ",";
           nxt++;
          }
          else
          {
           cout << abs*((input/1000000)%1000) << ",";
           nxt++;
           }
           
          if (input < -1000000)
          {
           abs = -1;
          }
}
if (input > 1000 || input < -1000)
{
          if (abs*((input/1000)%1000) < 100 && nxt > 1)
          {
           cout << "0" << abs*((input/1000)%1000)<<",";
           nxt++;
          }
          else
          {
           cout << abs*((input/1000)%1000)<<",";
           nxt++;
          }
         
          if (input < -1000)
          {
           abs = -1;
          }
          if (nxt > 1)
          {
           cout << "0" << abs*(input%1000);
          }
          else
          {
           cout << abs*(input%1000);
          }
}


cout << endl;

Yea, its long and obnoxious.
Logged
  • For The Swarm
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.322 seconds with 44 queries.