Like Tree1Likes
  • 1 Post By Elysia

question in cin(homework)

This is a discussion on question in cin(homework) within the C++ Programming forums, part of the General Programming Boards category; ok, heres my main Code: class BigNum{ public: char input[1000]; BigNum(); BigNum(char* ); char* convert(char *); int counter; }; BigNum ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Fukushima-shi, Fukushima, Japan, Japan
    Posts
    6

    question in cin(homework)

    ok, heres my main

    Code:
    class BigNum{
    public:
        char input[1000];
        BigNum();
        BigNum(char* );
        char* convert(char *);
        int counter;
    };
    
    BigNum output;    char num1[1000];
        char num2[1000];
        char *numm1;
    
    
        cout << "Enter first input"<< endl;
        cin >> num1;
        cout << endl;
        BigNum input1(num1);
        numm1 = input1.convert(num1);
        strcpy(num1, numm1);
    
    
        cout << "Enter second input"<< endl;
        cin >> num2;
        BigNum input2(num2);
        numm1 = input2.convert(num2);
        strcpy(num2, numm1);
    
    return 0;
    }
    
    BigNum::BigNum(char* input){
        strcpy(this->input, input);
    }
    
    
    BigNum::BigNum(){
        strcpy(input, "\0");
        counter = 0;
    }
    
    char* BigNum::convert(char* input){
        int temp, temp2, temp3;
    
    
        for(temp = 0;input[temp] != '\0';temp++);
        temp = 1000 - temp;
        for(temp2 = 0;input[temp2] != '\0';temp2++, temp++){
            input[temp] = input[temp2];
        }
        temp2 = 1000 - temp2;
        for(temp3 = 0;temp3 < temp2;temp3++){
            input[temp3] = '0';
        }
        input[1000] = '\0';
        return input;
    }
    the convert function simply moves the numbers in the string to the end of the string so it looks like from "123\0" into "00000123\0" where \0 is the end of string.

    my problem is during my second cin for the second input. when i display my first input right after the cin of the second input, the second input gets connected/combined with my first input. its like: if my first input is 123. my first input, after going through the convert function, would look like this 0000123. then i would put my second input, which would be 321. if i display my first input right after the cin of the second one, it would look like this 0000123321. why?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    A couple of notes:
    - Why are you using char arrays instead of std::string, or even a vector of char?
    - There seems to missing a piece between the class definition and the code (in main, I believe?).
    - Please adhere to this: SourceForge.net: Do not remove parameter names - cpwiki
    - Do not make everything in a class public. Only the interface that the outside should use to use the class.
    - You realize that your convert function actually modifies whatever you input? So after you do input1.convert(num1), num1 already contains the "converted" string. So your strcpy is useless. This is really dangerous, btw.
    Last edited by Elysia; 03-13-2013 at 10:45 AM.
    stahta01 likes this.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Question
    By Jozrael in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2008, 04:42 AM
  2. Homework question
    By Jozrael in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2008, 08:30 PM
  3. Homework Question...
    By djz3r0 in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 06:36 AM
  4. I have a homework question?
    By correlcj in forum C++ Programming
    Replies: 4
    Last Post: 10-03-2002, 10:38 PM
  5. I have a question about some homework?
    By correlcj in forum C Programming
    Replies: 4
    Last Post: 07-20-2002, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21