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?
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?
please?
Is your file really called "a:numbers.txt"? Maybe your file is on the a drive, in which case you should use:Or maybe your file is in the same directory as your executable, in which case you should use:Code:inFile.open("a:\\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.Code:inFile.open("numbers.txt");
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!
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?
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
Sorry, I just read the sticky. I'm really sorry!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; }
Last edited by deadkat; 10-19-2004 at 07:12 PM.
Well, that will work for 1 and 2.
Look:
works the same as:Code:switch (x) { case n1: // code block 1 case 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.Code:if (x == n1) // code block 1 else if (x == n2) // code block 2
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
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?
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.
But I'm required to use a switch statemnet.
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.
Okay, I aboslutely tried everything.
I really need the code now. Someone help me pretty please!
I need it right now! Pretty please, you guys!
Just tell me what am I doing wrong with switch statemnet?
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.