Thread: Can someone please tell me what is wrong with this code?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Can someone please tell me what is wrong with this code?

    I want to just take these numbers from the file one by one and assign them to different variables. I tried this just to see if I could get one to work and a get a crazy random number when it should just be 1.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {	double x;
    	ifstream input;
            input.open("chap10-Q1_input.txt");
    	input>>x;
    	cout<<x;
            return 0;
    }
    this is the file its connected to:
    Code:
    1  7  10  90 95
    2  9  8   90 80
    3  7  8   70 80
    4  5  8   50 70
    5  4  0   40 35
    Please help

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by cloudstrife910 View Post
    I want to just take these numbers from the file one by one and assign them to different variables. I tried this just to see if I could get one to work and a get a crazy random number when it should just be 1.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {    double x;
        ifstream input;
            input.open("chap10-Q1_input.txt");
        input>>x;
        cout<<x;
            return 0;
    }
    Please help
    Maybe you should check the return value of ifstream::open?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    What do you mean? How would I do that? Sorry I'm pretty new to this.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Sebastiani View Post
    Maybe you should check the return value of ifstream::open?
    ifstream::open doesn't return anything.
    But the thing is that, if the open fails, then the fail bit is set. Thus you can do:
    Code:
    if (input.fail())
    {
        // Code to handle failure here. Possibly exit application.
    }
    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.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    oh wow thank you, I did that. It wasn't opening correctly. I just assumed it was opening correctly cause the compiler didnt tell me. There was actually and underscore instead of a dash in the file name which messed it up.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Compilers don't say a thing about logic errors (except in some cases where it's smart enough to figure out you're shooting yourself in the foot, and then it's usually only when you have compiler warnings turned up pretty high) or failure to check return values and the like.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by cloudstrife910 View Post
    oh wow thank you, I did that. It wasn't opening correctly. I just assumed it was opening correctly cause the compiler didnt tell me. There was actually and underscore instead of a dash in the file name which messed it up.
    Yes, you will encounter this a lot. Getting stuff to compile is relatively easy. Getting it to actually do what you want is very hard. In general, if a function has a chance of failure (which you can easily see from the documentation) you should have some code that you do in such a case. At the very least print an error message so you know what went wrong.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or an assert.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM