Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

modernlifeislov

macrumors newbie
Original poster
Apr 29, 2010
3
0
Okay so I have already taken off the macroguard and done everything all of the other forums have said to do but my if statement if (!indata) will print "cout" in the terminal regardless if the condition is met or not.

My program runs fine, it outputs to the file okay. It's the problem with the terminal I'm concerned with.

he is my code; I have highlighted the code in question:

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;

const int BINARY = 2;
const int HEXADECIMAL = 16;

char DigitToChar (int digit);
// convert digit 0 to 16 to character
void ConvertNumber (int base, int number, ostream& outdata);
// convert decimal number to binary or hexadecimal
// writes result to outdata
int main()
{
	ifstream indata;
	ofstream outdata;
	
	indata.open("data_input.txt");
	outdata.open("data_output.txt");
	int number;
	[B]if (!indata)
	{
		outdata << "Missing Data Files!" << endl;
		[COLOR="Red"]cout << "Missing Data Files!" << endl;[/COLOR]
		system("PAUSE");
		return EXIT_FAILURE;[/B]
	}
	indata >> number;
	outdata << "Decimal          Binary          Hexadecimal" << endl;
	while(indata)
	{
		outdata << setw(5) << number;
		ConvertNumber(BINARY, number, outdata);
		outdata << " ";
		ConvertNumber(HEXADECIMAL, number, outdata);
		outdata <<endl;
		indata >> number;
	}
	indata.close();
	outdata.close();
	system("PAUSE");
	return EXIT_SUCCESS;
}
char DigitToChar (int digit)
{
	static char HexList[] = "0123456789ABCDEF";
	return HexList[digit];
}
void ConvertNumber (int base, int number, ostream& outdata)
{
    int digit;
    char chdigit;
    static int count;
	count = 0;
    
	if (number == 0)
		outdata << setw(12-count) <<" ";
	if (number > 0)
	{
		count++;
		digit = number%base;
		chdigit = DigitToChar(digit);
		ConvertNumber(base, number/base, outdata);
		outdata << chdigit;
	}
}

PLEASE HELP ME, THANK YOU
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
So you've run in the debugger, printed !infile at that line, and it's false, but one line of code in the if runs, but the others don't? Have you single-stepped through the code to see exactly what's being run?

-Lee
 

modernlifeislov

macrumors newbie
Original poster
Apr 29, 2010
3
0
It runs perfectly fine on Dev C++ in Windows. The problem is in xcode. I really really really don't want to go back to using parallels and windows, any help is appreciated
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
What exactly do you see happening?

It seems to work ok for me. I didn't use XCode - I used g++ in Terminal. Are you sure you have a data_input.txt?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Did you try what I said? Do you know what the workng directory that is being used is? Is your input file in that directory?

-Lee
 

modernlifeislov

macrumors newbie
Original poster
Apr 29, 2010
3
0
Here's everything that's going on

Screenshot2010-04-29at112809PM.png


it outputs to "data_output.txt" fine, but if you look in the terminal it still says missing data files.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
Code:
		system("PAUSE");

What do you expect this line to do? There is no PAUSE command on Mac OS X. This looks very Windows-specific.


Since your pathnames are relative to the working directory, I recommend that you print the value of the working directory, and confirm it is correct for the pathname your code is using. If it's not, then you need to change something.

You can get the working directory with the C function getcwd(). You can find its man page by searching Xcode's documentation for the function name.

If you don't understand the significance of the working directory, read this:
http://en.wikipedia.org/wiki/Working_directory
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
Let's see if I have this correct. From your posted image it looks like you've started the Terminal which defaults you to your home directory which looks to be '/Users/bloodysundayhc'. Then you attempt to execute your program by specifiy a "complete" path-name pair and it fails.

Try it in this order, change your working directory to the location of your executable first with:

Code:
cd /Users/bloodysundayhc/Desktop/Program\ 4\ CSC\ 210/build/Debug

Now that your current working directory is correct execute the program with:

Code:
./Program\ 4\ CSC\ 210
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.