ok so I have a program where the user inputs numbers from a keyboard. When they are done I have them press CTRL-Z, which on a PC represents EOF. Messing around with X-code, this is not the case. What do I have to press on a for OS X?
doesnt work.
#include <stdio.h>
int main (int argc, const char * argv[]) {
FILE *file_pointer ; /* declare the file handler / stream pointer. */
char input_char, last_char ;
file_pointer = fopen("memo.txt" , "r") ; /* this time read the file. */
if (file_pointer == NULL)
{
printf("File does not exist. Skipping the READ.\n") ;
}
else
{
printf("The old contents of memo.txt are:\n") ;
while (input_char != EOF )
{
input_char = getc( file_pointer ) ;
if (input_char != EOF) printf("%c" , input_char ) ;
}
printf("\n...End of memo.\n") ;
fclose(file_pointer) ;
}
file_pointer = fopen("memo.txt", "w") ; /* open the file. Get a pointer. */
printf("Enter Memo...\n") ;
int double_enter = 0 ;
input_char = ' ' ;
while ( double_enter==0 ) /* do it until enter is pressed twice. */
{
if (last_char == '\n' && input_char == '\n') double_enter = 1 ;
else
{
last_char = input_char ;
scanf("%c", &input_char) ; /* must pass the address of the variable */
putc( input_char, file_pointer) ; /* putc updates file_pointer with the next address when finished. */
}
}
fclose(file_pointer) ;
return 0;
}
yaya, i keep screwing up the name, sue me.
thx for the help though
edit:Control-D would not do it
Yes, that weird character is a side effect of using weird emacish escape sequences to squeeze control characters into the stdin stream.the control q and control d procude a ¿
okay, now I'm getting suspicious about the general use of EOF in this program, whether or not it's even the right thing to do (it may accidentally do the right thing under Windows just as a side effect).and then whole program runs but does not stop at my next function like it is supposed too.
Correct, that won't work inside the Xcode log window.newline and control-d does nothing.
#include <iostream> //This is our prototyping and global
using namespace std ; //declarations
void display(int[], int) ;
int average(int, int, int[]) ;
void search(int[], int) ;
void sort(int, int, int, int, int[], int) ;
int largest(int, int[]) ;
int smallest(int[]) ;
int main() //This is the main function where all
{ //other functions are called to.
int array[50] ;
int actsize = 0 ; //Declartion and instatiation of
int sum = 0 ; //variables
int arrayLength = 0 ;
int passactsize = 0 ;
int searchIndex = 0 ;
int minIndex = 0 ;
int choice ;
cout << "Please choose one of the following" << endl ; //These are the Menu
//options
cout << "Option 1 input from keyboard" << endl ;
cout << "Option 2 quit " << endl ;
cin >> choice;
if(choice = 1) //If the user choses choice 1 we enter the if
{ //statement
switch(choice)
{case 1:
cout << "Welcome to Oliver's Number Array Program" << endl ; //Welcoming statements
cout << "Please follow the directions carefully" << endl ;
cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
//Prompting user for keyboard input
while(cin) //Takes the input and puts into an array
{
cin >> array[actsize] ;
actsize++ ;
}
cin.clear() ;
actsize--; //Subtract element so that end of file is not displayed in array.
cout << "The Array will now be displayed to you" << endl ; //Array will now be displayed
display(array, actsize) ; //Display array Function
cout << "The average of the Array will now be computed" << endl ;
cout << "The average is "; //Average of array will be found
if (actsize > 0)
cout << average(sum, actsize, array) << endl ; //Calls the average function
else
cout << "zero" << endl ; //Output if there is no average
cout << "You will now be given an opportunity to search the Array" << endl ;
//User is told they may search the array
search(array, actsize) ; //Calls search function
cout << "The Array will now be sorted" << endl ; //User is told the array will be sorted
//using a selection sort from the book
sort(arrayLength, passactsize, searchIndex, minIndex, array, actsize) ; //Calls the sort function
cout << endl; //Some space to make it look nicer
cout << endl;
cout << endl;
cout << endl;
cout << "The Array will now be displayed to you" << endl ; //Tells user that Array will be displayed
display(array, actsize) ; //Calling display function
cout << "The largest value in the array is" << endl ; //Tells user that the largest value will be
//Displayed
largest(actsize, array) ; //Calling largest function
cout << array[actsize-1] << endl ;
cout << "The smallest value in the array is" << endl ; //Tells user that the largest value will be
//displayed
smallest(array) ; //Calling smallest function
cout << array[0] << endl ;
}}
else
cout << endl; //if choice 2, then the main is not entered
}
//Purpose: This function is used to dispaly the array to the user
//Pre: Waiting to be called to read the array
//Post: Function reads and displays the array
void display(int array[], int actsize)
{
cout << "Index" << " " << "Arrayelement" << endl ;
for (int L = 0 ; L < actsize ; L++)
cout << L << "." << " " << array[L] << endl ;
}
//Purpose: This function calculates the average of the array
//Pre: Waiting to be called to read values from array and take average
//Post: Average of the array is calculated and sent back to the main
int average(int sum, int actsize, int array[])
{
sum = 0 ;
for (int L = 0 ; L < actsize ; L++)
sum = sum + array[L] ;
return sum / actsize ;
}
//Purpose: To give the user an opportunity to search the array
//Pre: Waiting to be called to read and compare values of the array
//Post: Array is searched and values are compared to see if the target is in the Array
void search(int array[], int actsize)
{
float target ;
int t = 0 ;
cout << "Enter a number to search for ";
cin >> target ;
while(t < actsize && array[t] != target)
t++ ;
if (t != actsize)
cout << target << "is in the array" << endl ;
else
cout << target << "is not in the array" << endl ;
}
//Purpose: This is a selection sort from the book to sort the array in ascending order
//Pre: Waiting to be called to sort the array in ascending order
//Post: Array is now sorted in ascending order
void sort(int arrayLength , int passactsize, int searchIndex, int minIndex, int array[], int actsize)
{
int temp ;
arrayLength = actsize ;
for (passactsize = 0 ; passactsize < arrayLength ; passactsize++)
{
minIndex = passactsize ;
for (searchIndex = passactsize +1 ; searchIndex < arrayLength; searchIndex++)
{
if (array[searchIndex] < (array[minIndex]))
minIndex = searchIndex ;
}
temp = array[minIndex] ;
array[minIndex] = array[passactsize] ;
array[passactsize] = temp ;
}
}
//Purpose: Find the largest value in a sorted array
//Pre: Waiting to be called to display the largest value in array
//Post: The largest value is found and sent back to main
int largest(int actsize, int array[])
{
return array[actsize-1] ;
}
//Purpose: Find the smallest value in a sorted array
//Pre: Waiting to be called to display the smallest value in array
//Post: The smallest value is found and sent back to main
int smallest(int array[])
{
return array[0] ;
}
the program said:[Session started at 2007-02-08 00:57:19 -0500.]
Please choose one of the following
Option 1 input from keyboard
Option 2 quit
1
Welcome to Oliver's Number Array Program
Please follow the directions carefully
Enter the value, press enter, and repeat as needed. When done press CTRL-Z
1
2
3
(here I typed ctrl-q ctrl-d return)
The Array will now be displayed to you
Index Arrayelement
0. 1
1. 2
2. 3
The average of the Array will now be computed
The average is 2
You will now be given an opportunity to search the Array
Enter a number to search for -2.26144e-29is not in the array
The Array will now be sorted
The Array will now be displayed to you
Index Arrayelement
0. 1
1. 2
2. 3
The largest value in the array is
3
The smallest value in the array is
1
numbers has exited with status 0.
Okay, I found some oddness in the GCC libraries (it doesn't seem to be specific to Mac either, I turned up a bug report for djgpp with the same problem). The clear() method doesn't really reset eof.
As a workaround, you'll need to use both:
cin.clear();
clearerr(stdin);
=-=-=-=
While the above will make the program work, it's really not a good way to be doing things, it's a little too device dependent. This search for EOF and then waiting for more input will fail if stdin is an actual file, ctrl-D only has a special meaning (one that can be undone) on interactive streams. The Right Thing to do is look for a non-numeric entry (or some specific value) for the end of the number sequence.
PS. If you have time could you tell me what clearerr(stdin); is?
I still say the best idea is to look for an empty line and use that to end the list. Relying on control characters is not really a safe cross platform approach. You should invest some time to check out the sample code that toddburch presented...its a safe alternative.
Keep in mind that when you are debugging code in XCode, its not really running under the same circumstances as it would outside of XCode. Dubuggers have a sneaky way of intercepting certain inputs.