quick c++ question

Discussion in 'OT Technology' started by notwist, Mar 7, 2008.

  1. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    I thought it would be fun to take up programming in C++. I've taken classes for it before but just forgot. Anyways, I have a quick question. Say I write a program to simply open a file (say, a text file filled with random numbers) and then read the first 5 digits. How do I take those 5 numbers and "convert" them to integers? I realize you can simply subtract from them based on the ASCII code but what if you don't know the numbers? Basically, how do I read some numbers from a text file (using getline or get) and "convert" them to integers?
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    the atoi() function
     
  3. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    i have a txt file full of numbers and i just want to take the first 5 digits, save it has one number, shift over 1 digit, take another 5 digits, save it, and so on until the end of the file

    here is the code
    Code:
    	fstream Prime;
    	Prime.open("digitsofe.txt");
    	
    	if (!Prime.is_open()) { cout << "unable to open"; }
    	else if (Prime.is_open()) {
    		cout << "File opened successfully\n";
    		Prime.getline(str2,6);
    		cout << str2 << endl;
    		Prime.seekg(1);
    		Prime.getline(str2,6);
    		cout << str2;
    		
    		Prime.close();
    }
    right now i'm just trying to print the first 5 digits and then the next 5 digits but the second number won't print. what am i doing wrong?

    I tried using the get function but using atoi kept giving errors.
     
  4. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    Please, don't post the errors.
     
  5. wabash9000

    wabash9000 Active Member

    Joined:
    Dec 27, 2006
    Messages:
    14,392
    Likes Received:
    0
    Location:
    Overland Park, KS
    I could way over do it if you wanted me to:

    First set up a queue. Then push the first 5 chars from the txt file on to the queue. Make a while loop with some conditional that you can figure out. Then pop the queue, convert the char to an int and add it to the sum. Then for loop 0 to < 4, then pop the queue, convert the char to an int and add it to the sum, then push the char back onto the queue for the next round. Do what ever you want with the sum at this point. Now push 1 more char from the txt file and loop to the while.

    probally not the easiest way but I can garentee it will work, and if you can figure it all out you will be a better coder.
     
  6. CodeX

    CodeX Guest

    atoi() or subtract the ASCII base from the character... like you already said or have been told... you keep getting answers but then reposting the same question.

    Do you need the answers explained further?
     
  7. aethyr

    aethyr OT Supporter

    Joined:
    Aug 27, 2007
    Messages:
    160
    Likes Received:
    0
    Location:
    Cali
    Don't use getline(), use read() instead.
     

Share This Page