Thread: Why there is a difference between working of these nearly same codes involving input?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Why there is a difference between working of these nearly same codes involving input?

    Code:
    #include<iostream>     //Call it 1st 
     
    using namespace std;
    
    
    int main()
    {
        int i=019;
        cout<<i<<endl;
        return 0;
    }
    It throws an error as I have started initialization with a 0 which makes it octal but then 9 is not an octal.

    Code:
    #include<iostream>     //Call it 2nd 
     
    using namespace std;
    
    
    int main()
    {
        int i;
            cin>>i;
        cout<<i<<endl;
        return 0;
    }
    It works fine even when the input is 019? --> why is there any difference in functioning?
    +
    I got this that method of solving error due to combination of octal & 9 is to take input from cin but is there any other method?
    +
    How can I give an input to cin in octal?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,615
    Use std::oct

    if (cin >> oct >> i)
    cout << "Number okay " << i << endl;
    else
    cout << "Ooops" << endl;

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    If you're looking for a prefix-dependent parse of the input value (like "%i" of scanf), then:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main() {
        // 0 for prefix-dependent, 8 for oct, 10 for dec, 16 for hex
        std::cin >> std::setbase(0);
    
        std::cout << std::showbase; // set cout to print 0 before octal, 0x before hex
    
        for (int n = 0; std::cout << ">> ", std::cin >> n; )
            std::cout << "<< "
                      << std::oct << n << ' '
                      << std::dec << n << ' '
                      << std::hex << n << '\n';
    
        std::cout << '\n';
        return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Some error handling will be needed - try to enter 09 to this code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by vart View Post
    Some error handling will be needed - try to enter 09 to this code
    Maybe you should have tried it yourself first. ;)

    Obviously error-handling has been left out. However, 09 is interpretted as two separate entries, 0 followed by 9. That's not necessarily an "error". It depends on the behaviour you want. In general the usual advice applies, that you should read an entire line first and process that with sscanf in C or istringstream in C++, possibly checking for extraneous non-whitespace characters after a value is read.

    Also note that the std:cin controlling the for loop will fail if it can't interpret the input, causing the loop to terminate. E.g., an input of z will cause it to terminate. Again, whether that's an "error" depends on your needs. Here it hardly matters and at least we don't end up in an infinite loop.

    Anyway, my main point is that there is a prefix-dependent option (setbase(0)) which is not obvious if you only consider std::oct, std::dec, and std::hex. I.e., there is no std::prefix_dependent (or whatever) to round out that group.

    Here's a version that reads a line at a time, doesn't accept extraneous characters, and doesn't quit on bad input.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    
    int main() {
        std::string line;
        while (std::cout << ">> ", getline(std::cin, line)) {
            std::istringstream iss(line);
            iss >> std::setbase(0);
            long n = 0;
            if (!(iss >> n))
                std::cout << "bad input\n";
            else {
                std::string s;
                iss >> s;
                if (s.size() > 0) // extraneous non-whitespace character(s)
                    std::cout << "bad input\n";
                else
                    std::cout << std::showbase << "<< "
                              << std::oct << n << ' '
                              << std::dec << n << ' '
                              << std::hex << n
                              << std::noshowbase << '\n';
            }
        }
        std::cout << '\n';
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    One solution I got to take the input as String and then make a function to do the conversion job somewhat on the lines on atoi function but with base 8 for octal.

    Apart from this can you please explain what is this line of code you have written doing and how?
    std::cout << ">> ", std::cin >> n;

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by gaurav# View Post
    can you please explain what is this line of code you have written doing and how?
    That's not a very specific question. What part of it do you not understand? Do you know what cout does? Do you understand how the << operator works in that context? Do you know what a string literal is? Do you know what the comma operator does? Do you know what cin does? Do you understand how the >> operator works in that context? Do you know what an integer is? Do you understand how the test portion of a for loop works? What don't you understand?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>std::cout << ">> ", std::cin >> n;
    That's equivalent to:
    std::cout << ">> ";
    std::cin >> n;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Elysia View Post
    >>std::cout << ">> ", std::cin >> n;
    That's equivalent to:
    std::cout << ">> ";
    std::cin >> n;
    It's not equivalent in the context it came from (the test portion of a for loop).

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    I understand the cin-cout part but the comma seprating them is not clear, what functionality it adds?

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,615
    When a comma separates two or more expressions, absent any other context, the comma is an operator: the expressions are treated like statements, and executed from left to right, with the rightmost expression being the result.

    So when you read this:
    Code:
    while(std::cout << ">> ", getline(std::cin, line))
    The while loop will use getline's return value as its condition. Since the return value is an istream object, (std::cin,) an implicit conversion to Boolean type happens first.

    The same thing happens in the other for loop that algorism posted, except there he used the std::cin object directly.
    Code:
    for (int n = 0; std::cout << ">> ", std::cin >> n; )
    Most places you see the comma, such as array initializations or function argument lists, are not a use of the comma operator, so again, context matters.

    If it's not already apparent, such constructions are only strictly necessary because of where they occurred in the loop. There are ways to write the loop that will have no use of the comma operator at all.
    Last edited by whiteflags; 02-20-2017 at 07:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My codes working correctly, nevertheless I get debug error
    By ilerleartik in forum C Programming
    Replies: 4
    Last Post: 03-17-2012, 09:25 AM
  2. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  3. Difference between these two codes
    By dnguyen1022 in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2009, 02:47 AM
  4. Difference between two codes(very basic)
    By subhasnagre in forum C Programming
    Replies: 6
    Last Post: 08-25-2008, 02:08 AM
  5. Replies: 7
    Last Post: 01-22-2008, 09:58 AM

Tags for this Thread