Putting a "fgets( buf, BUFSIZ, stdin );" in every case solved that, but that's a lot of duplicate code. I'm thinking there is a better way to do it?
Type: Posts; User: never_lose
Putting a "fgets( buf, BUFSIZ, stdin );" in every case solved that, but that's a lot of duplicate code. I'm thinking there is a better way to do it?
Hmm, I've got a pretty good solution now, but not completely error proof.
Look what happens if I enter "1 3".
#include <stdio.h>
#include <stdlib.h>
//#define BUFSIZ 100
So what is it returning if I enter, say, 2?
What does the 1 represent, and why would I have a problem if it's returning something other than 1, if the while loop is testing for everything other...
while( scanf( "%d", &exercises ) != 1 )
What is this checking?
I should've said from the beginning that the program would be a continuous loop, but I didn't think the loop would add such complications. My fault...
int main(int argc, char** argv) {
int...
How do I keep 'the crap' out of the read buffer?
I tried quzah's option at first, but then when I tried to enter something, it would go to a new line. So I guess I wasn't putting it inside my code...
I like CommonTater's solution. But it still doesn't work quite right.
My program continuously loops.
int main(int argc, char** argv) {
int exercises;
int exercises;
scanf("%d", &exercises);
switch (exercises) {
case 1: printf("1"); break;
case 2: printf("2"); break;
case 3: printf("3"); break;
...
The values of the characters are stored in contents[]. So by "mapping them to an array index" do you mean I have to store them in another array?
What is an array index, and how would I map a character into one?
I have to count the frequency of each character. Do I need an array then?
I was going to storing them in an array because I was going to compare all the characters to see if they are the same.
If I want to compare the characters, I would have to store them in an array,...
Thanks. I knew it had something to do with the fgetc() being at the EOF but I wasn't sure how to go back to the beginning.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(int argc, char** argv) {
FILE *textfile;
int max_chars = 1000;
Thanks, puts() and fread() is what I was looking for. And yeah, I'll have to close the file when I'm done. Thanks for the help.
EDIT: Oops, I meant puts().
I'm simply trying to print out the contents of a text file to make sure I am in fact opening and reading a text file correctly. I also want to store all the characters in an array. Here's my code so...
Wow. I got it, haha. Thanks!
scanf("%d",&num);
I can use this to get positive numbers from the user easily.
But say I also want the user to enter negative numbers, like "-10."
Obviously I would have to check and see if...
#include "readline.h"
This is at the top of a code that I'm trying to run, but I can't run it because I get this error:
I'm using NetBeans on Windows 7. I've downloaded the readline 6.2...
Ah, thanks! I didn't think fprintf would actually write into a file! Interesting. :D
Say I have a code like this:
int main() {
int a=1;
int b=2;
int x=3;
int y=4;
Ok, thanks... and what is the purpose of (void) getchar();?
Never mind, it must be so that the user can "press enter when ready."
I've finally finished my program by the way, thanks for the...
I'm on Windows. Nothing appears after I hit ctrl+z.
But when I run this on an Unix machine from my Windows computer it works.
Any idea why it won't work in Netbeans?
Tried Ctrl+Z. After that, enter does nothing, I can't type anything, and I can't move the insertion point anywhere.
I'm using Netbeans. When I compile, it opens in command prompt.
In your code, how do I enter after I type something? Pressing enter only goes to a new line.