-9

I am trying to return long int from a function but it is not working at all, then I tried to print a long int number to the screen and still not working.

#include<iostream>
using namespace std;

long int maximum_product(long int *input_array, int n){
    long int a,b;
    a = *input_array;
    b = *(input_array + 1);
    if (b > a){
        long int temp = a;
        a = b;
        b = temp;
    }

    for (int i = 2; i < n; i++){
        if (input_array[i] > b){
            if(input_array[i] > a){
                b = a;
                a = input_array[i];
            } else
                b = input_array[i];
        }
    }
    return a * b;
}

int main(){
    int n;
    cin >>n;
    long int *input_array = new long int[n];
    for (int i = 0; i < n; i++)
        cin >> input_array[i];
    cout << maximum_product(input_array, n);
    return 0;
}

Here is what I mean by "not working":

#include<iostream>
using namespace std;

int main(){
    long int y;
    cin >>y;
    cout<<y;
    return 0;
}

Result of the second program

  • 7
    "Not working" is a poor description of your problem. Please describe what you expect, what you observe and how they differ. It's not clear what the problem is. – François Andrieux Aug 27 '18 at 14:39
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 27 '18 at 14:40
  • I would have chosen more descriptive names for `a` and `b` – drescherjm Aug 27 '18 at 14:43
  • 2
    try the input without the commas. And please try to avoid screenshots. Text can be copied and pasted, but my console doesnt accept images as input – 463035818_is_not_a_number Aug 27 '18 at 14:47
  • The size of 32-bit signed integer is from -2^(32) to 2^(32) - 1, but the problem is that I really don't know how to use (long int, unsigned long int, double, ..) whenever I use those I get the result in the image. – Hassan Ibrahim Aug 27 '18 at 14:48
  • https://www.laptopmag.com/articles/how-to-windows-10-command-prompt-copy – drescherjm Aug 27 '18 at 14:49
  • 2
    the problem in your last example is that `2,....` is parsed as `2`. You cannot use `,` as seperator when you use `std::cin` – 463035818_is_not_a_number Aug 27 '18 at 14:50
  • It seems the regional settings for your system doesn't include thousand-separator. – Some programmer dude Aug 27 '18 at 14:53
  • OT: Don't post screenshots of text; copy-paste text. – Algirdas Preidžius Aug 27 '18 at 15:10
  • @user463035818 _You cannot use `,` as seperator when you use `std::cin`_ Oops. I just wrote an answer where I showed how to do it [SO: input day,month,year and store in a seperate structure](https://stackoverflow.com/a/52000352/7478597). (And, to be fair, I just copied it from another answer [SO: Read file line by line](https://stackoverflow.com/a/7868998/7478597).) – Scheff's Cat Aug 27 '18 at 15:11
  • 1
    To sort out this kind of problem, begin by isolating the issue. There are two steps here: read a bunch of input, and calculate the result. To isolate the issue, get rid of the input, and simply assign values to the array. See if that works. If it doesn't, the problem is in the calculation; if it does work, the problem is in the input. – Pete Becker Aug 27 '18 at 15:11
  • Tip: you can execute those commands in one line: `g++ my_test.cpp && a` so that it will run the exe iff it successfully compiled, and you can use the up arrow to recall previous commands. – Boann Aug 27 '18 at 16:00
  • @Scheff well you can do it if you read the seperator "manually" but you wont get it converted automatically to a `long`. Actually I was afraid that someone would prove me wrong, but reading the sepeartor seperately I consider as cheating :P – 463035818_is_not_a_number Aug 28 '18 at 07:52
  • @user463035818 Even better cheating would be to change the delimiter. This is rather trivial for [`std::getline()`](https://en.cppreference.com/w/cpp/string/basic_string/getline) where it's just a parameter (which is usually left at default) but it's even possible for input with `operator>>()` as I just learned here: [SO: changing the delimiter for cin (c++)](https://stackoverflow.com/a/7304184/7478597). (Though, I wouldn't recommend this technique for an entry level programmer, and I myself would prefer to simply write a [parser](https://stackoverflow.com/a/50021308/7478597).) ;-) – Scheff's Cat Aug 28 '18 at 07:59
  • @Scheff i was curious, so I tried to do it somehow without reading too much elsewhere. My answer is more a case-study than an answer, but actually I really would prefer to write an `operator>>` than changing the state of the stream globally as suggested in one of your links – 463035818_is_not_a_number Aug 29 '18 at 07:53

1 Answers1

0

If you want to make std::cin read

2,147,483,646

as a long you need to do a little bit extra, because without you std::cin will only read the first 2 and ignore the rest.

Lets start simple....std::cin has an overload of its operator>> for long but we want it to do something else. How do we make it pick a different overload? We pass a different type. However, as we dont actually want anything else than a long we use a thin wrapper:

 struct read_long_with_comma_simple_ref {
     long& long;
     read_long_with_comma_simple_ref(long& value) : value(value) {}
 };

Once we supply our own operator>> overload we will be able to write something like this:

long x;
std::cin >> read_long_with_comma_simple_ref(x);

So far so simple. How can we implement that operator>>? The most simple I can think of is to simply ignore the commas:

std::istream& operator>>(std::istream& in,read_long_with_comma_simple_ref& ref) {
    std::string temp;
    // read till white space or new-line
    in >> temp;
    // remove all commas   
    temp.erase(std::remove(temp.begin(), temp.end(), ','), temp.end());
    // use a stringstream to convert to number
    std::stringstream ss(temp);
    ss >> rwcs.value;
    return in;
}

However, this will accept non-sense such as 12,,,34 as input and interpret it as 1234. This we of course want to avoid and add a bit of error checking:

#include <iostream>
#include <limits>
#include <algorithm>
#include <sstream>
#include <ios>

 // same as before, just different name for a different type
 struct read_long_with_comma_ref {
     long& long;
     read_long_with_comma_ref(long& value) : value(value) {}
 };

 std::istream& operator>>(std::istream& in,read_long_with_comma_ref&& rwc){
     std::string temp;
     in >> temp;
     std::reverse(temp.begin(),temp.end());
     std::stringstream ss(temp);
     rwc.value = 0;
     long factor = 1;
     for (int i=0;i<temp.size();++i){
         char digit;
         ss >> digit;
         if (((i+1)%4)==0) {
             if (digit != ',') {
                 in.setstate(std::ios::failbit);
                 rwc.value = 0;
                 return in;
             }
         } else {
             int dig = digit - '0';
             if (dig < 0 || dig > 9) {
                 in.setstate(std::ios::failbit);
                 rwc.value = 0;
                 return in;
             }
             rwc.value += factor* (digit-'0');
             factor*=10;
         }
    }
    return in;
}

And we can use it like this:

long x;
std::cin >> read_long_with_comma_ref(x);
if (std::cin.fail()) std::cout << "reading number failed \n";
std::cout << x;

TL;DR if you want to make std::cin read the value 2147483646 then simply type 2147483646 and dont use the , as seperator, or read it as string and remove the commas before converting it to a number.

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
  • I'm not sure whether OP intends to recognize separator to separate numbers or as thousands separator. (May be, the snapshot answers this but I cannot open due to company's firewall. So far about snapshots...) Meanwhile, I found [SO: Print integer with thousands and millions separator](https://stackoverflow.com/a/17530533/7478597). Though, both answers are based again on `std::imbue`. – Scheff's Cat Aug 29 '18 at 08:48
  • @Scheff oh, yes the snapshot answers that. Seems like OP found out that 2,147,483,646 is their max long and tried to pass it to `cin` – 463035818_is_not_a_number Aug 29 '18 at 09:09
  • @Scheff hm this looks much simpler than what I came up with ;). Still I think I would wrap the `imbue` in my own `operator>>`, somehow I dont like the idea of messing around with the stream globally – 463035818_is_not_a_number Aug 29 '18 at 09:11