Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. something wrong with code???

something wrong with code???

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajveer
    wrote on last edited by
    #1

    Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1

    H D C 3 Replies Last reply
    0
    • R Rajveer

      Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1

      H Offline
      H Offline
      Hans Ruck
      wrote on last edited by
      #2

      Your program does the equivalent of atoi("1,2,3,4,5,6") when enters infile>>array. You have to use space or CR to separate the numbers, not commas. rechi

      R 1 Reply Last reply
      0
      • R Rajveer

        Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1

        D Offline
        D Offline
        dlhson
        wrote on last edited by
        #3

        I think something wrong in your computer or your OS. Because in my computer, it's right. Output is: 1 Hung Son A Vietnamese student i-g.hypermart.net dlhson2001@yahoo.com

        1 Reply Last reply
        0
        • H Hans Ruck

          Your program does the equivalent of atoi("1,2,3,4,5,6") when enters infile>>array. You have to use space or CR to separate the numbers, not commas. rechi

          R Offline
          R Offline
          Rajveer
          wrote on last edited by
          #4

          okay... I just changed the file "binga" so it looks like this: 1 2 3 4 5 6 a b c d e f one two three four five six I ran the program again and I still got the output as -858993460 as opposed to what it clearly should be: 1 what is going on here??

          1 Reply Last reply
          0
          • R Rajveer

            Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Your main problem is simply that it's poorly written code. For one, void main is NOT valid C++. For another, you don't initialise your variable, and for a third, you don't check to see if you've succeeded in opening a file, which you give only a relative path to.

            int main()
            {
            int array = -1;
            ifstream infile ("c:\binga");

            if (infile.isopen())
            {
            infile >> array;
            cout << array << endl;
            }
            else
            cout << "Unable to open file";
            }

            I think you'll find it cannot open/find the file and is giving you the initial value of array. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups