getting info from a file [ifstream]

This is a discussion on getting info from a file [ifstream] within the C++ Programming forums, part of the General Programming Boards category; I have three numbers saved in a txt file named numbers.txt and I use this code: inFile.open("a:numbers.txt"); inFile>>num1>>num2>>num3; but it ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    10

    getting info from a file [ifstream]

    I have three numbers saved in a txt file named numbers.txt

    and I use this code:

    inFile.open("a:numbers.txt");
    inFile>>num1>>num2>>num3;


    but it doesn't work, what am I doing wrong? Should I use getline, if so, I'm lost as to how to use it here?

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    please?

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Is your file really called "a:numbers.txt"? Maybe your file is on the a drive, in which case you should use:
    Code:
    inFile.open("a:\\numbers.txt");
    Or maybe your file is in the same directory as your executable, in which case you should use:
    Code:
    inFile.open("numbers.txt");
    The way you are reading in the numbers with operator>> should work fine. You should only use getline if you want a string of text including the spaces, but since numbers don't have spaces in them, it isn't necessary.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    Yeah, I think it's just the location!

    Ahh, so even if the there are several numbers, I still read it in the regular way? Thank you very much!

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    Okay, I have another question concerning the switch statement:

    switch ( x )

    {
    case 1:
    if ((x%2)!=0)
    cout<<"The number is odd";
    break;



    case 2:
    if ((x%2)==0)
    cout<<"The number is even";
    break;


    }

    I'm using this code - but again I'm having a problem with it, it asks the user to input an integer, but does not display any of the cases?

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    We'd really need to see more of the code. It looks like you're using x without assigning it a value. Read a bit more about the switch statement.

    BTW, use code tags.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    Code:
    int x;
    	cout<<"Enter an integer number: "<<endl;
    	cin>>x;
    	
    	
    	
    	switch ( x ) 
    	
    	{
    	case 1:
    		if ((x%2)!=0)
    			cout<<"The number is odd";
    		break;
    	 
    	 
    
    	case 2:
    		if ((x%2)==0)
    			cout<<"The number is even";
    		break;
    	
    		
    	}
    Sorry, I just read the sticky. I'm really sorry!
    Last edited by deadkat; 10-19-2004 at 08:12 PM.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Well, that will work for 1 and 2.

    Look:
    Code:
    switch (x)
    {
       case n1:
          // code block 1
       case n2:
          // code block 2
    }
    works the same as:
    Code:
    if (x == n1)
       // code block 1
    else if (x == n2)
       // code block 2
    If you want to use a switch statement, you'll want your switch condition to be a little more meaningful than just the input number.

    It would be easier to just use one of your if statements and cout the desired output by adding an else clause.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    Okay, excuse my slowness - it's 5 am where I live.

    So in the case n1, and case n2 - what do I exactly type for a condition?

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your code is technically correct as is... there is something else wrong in your code if you are not getting any output.

    Logically, your code could use some work. In case 1, you know the number is 1, so there is no need to check if ((x%2) != 0). You know 1 is odd. Same for case 2. For displaying whether a number is odd or even, you do not need a switch. Simply put the two if statements into the code after the input.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    But I'm required to use a switch statemnet.

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Well, I can think of one way to use the switch statement for this assignment, but I won't tell you flat out.

    First, you should figure out why your code isn't working, like I said it is technically correct.

    Then, consider using something other than x for your switch statement. Think about a number you can get from x that has to be one of only a few possible values and that can help you when determining if the input is even or odd.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    Okay, I aboslutely tried everything.

    I really need the code now. Someone help me pretty please!

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    10
    I need it right now! Pretty please, you guys!

    Just tell me what am I doing wrong with switch statemnet?

  15. #15
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi deadkat,

    Maybe this will help a little...

    You need to apply %2 to every possible x entry, right? But it's impossible to create a seperate case in a switch statement for every possible x entry, right? Look at where your x%2 is now. It's in the switch statement.

    Don't give up! Keep working at it, you'll get it eventually.
    Best of luck.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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