C/C++ Thread

Cromewell

Administrator
Staff member
Your functions work and the rest of the code seems to execute fine (I had to correct a , in your main that should be a ; but that's just a minor typo).

One thing about your functions, since arrays are passed as a pointer you should be passing the length of the array. What if you wanted more than a 52 card deck? You'd have to update lots of 52s in your code.

Ideally, you should be using a function to ask each player to hit or stay, its repeated an awful lot.

Also, since you're using C++ so you can simplify things by making objects and passing them around. i.e. a player object, a deck object, card object etc. This way, each player can have a hand object (or whatever) which consists of say an array of cards and some methods to figure out what you are allowed to do i.e. no more hitting once you've busted.
 

galerecon

New Member
Your functions work and the rest of the code seems to execute fine (I had to correct a , in your main that should be a ; but that's just a minor typo).

One thing about your functions, since arrays are passed as a pointer you should be passing the length of the array. What if you wanted more than a 52 card deck? You'd have to update lots of 52s in your code.

Ideally, you should be using a function to ask each player to hit or stay, its repeated an awful lot.

Also, since you're using C++ so you can simplify things by making objects and passing them around. i.e. a player object, a deck object, card object etc. This way, each player can have a hand object (or whatever) which consists of say an array of cards and some methods to figure out what you are allowed to do i.e. no more hitting once you've busted.

The reason you found a , instead of ; is because I deleted a variable that was useless at this time when posting here.

Yes, this was the second time I was told that. When you say object you mean class for each right? The problem with that is that I don't fully understand classes. And I was writing this for my lab in my Computer Science 1 class. Our teacher is terrible so almost all of this is what I learned by myself over the past few weeks.

Do you have any suggestions for a way to count the sum using my code that I wrote? This lab is due today, but I will be rewriting this over winterbreak using classes and such. This code was also written at 12am-8am. (Welcome to college). Thanks for the help.
 
Last edited:

Cromewell

Administrator
Staff member
I figured the comma was something like that, it's not a big deal that stuff happens :)

Yes I mean classes.

To make it work with what you have, you need to track the cards each person has. Right now, I'm not seeing anything that does that. You can use more arrays for now if you aren't comfortable enough with classes yet. For each hit/stay loop you'd need to push the drawn card value to the players array. Kind of ugly but it will work.
 

galerecon

New Member
I figured the comma was something like that, it's not a big deal that stuff happens :)

Yes I mean classes.

To make it work with what you have, you need to track the cards each person has. Right now, I'm not seeing anything that does that. You can use more arrays for now if you aren't comfortable enough with classes yet. For each hit/stay loop you'd need to push the drawn card value to the players array. Kind of ugly but it will work.

Thanks i appreciate the help.
 

neilofbodom

Member
Hi guys

I have a C problem. I need to write a program which asks the user to input information about customers (to keep it simple, just name, surname and address). After the user input, I need to write that data to a text file.

I got this so far:

CreateCustomer()
{

char name[30];
char surname[30];
char address[100];

FILE *customers;


printf("New Customer Page. \n");
printf("Please enter the customer's name. \n");
scanf("%s",name);

printf("Please enter the customer's surname. \n");
scanf("%s",surname);

printf("Please enter the customer's address. \n");
scanf("%s",address);

customers = fopen("Customers.txt","w");
fprintf(customers,"%s %s %s",name,surname,address);
fclose(customers);


}

After running the program (i'm running on Ubuntu so I'm using the terminal to compile and run my programs), and entering some test data, this error comes up: Segmentation fault (core dumped).

Am i doing the writing correctly? If not, what is the problem? Keep in mind that I just started C at University a couple of months ago, and this stuff is still new to me.

Also, just because I am curious, what does that error mean?

Cheers!
 

Troncoso

VIP Member
A Segmentation Fault, as a basic explanation, is an error that is thrown when you try to access something in a way that you can't. Usually from using pointers in the wrong way.

I think the issue is the way you are trying to store the user input into the char pointers (the arrays). Try something like this:

Code:
CreateCustomer()
{

char name[30];
char surname[30];
char address[100];

FILE *customers;

customers = fopen("Customers.txt","w");
fprintf(customers,"This is a test string");
fclose(customers);
}

If that works, than my assumption is correct. If you search for "read user input into char array" in Google, you'll find that most people use a method of grabbing each char individually, all the while checking for the EOF character.
 

TrainTrackHack

VIP Member
Don't know if this is causing the issue, but you're only supposed to use printf for formatted data. For strings without formatting, it's better to use puts.

Also, you could use a debugger or if that's a bit too much, just comment out the lines and uncomment them one by one to see which line is causing the issue.
 

Cromewell

Administrator
Staff member
I believe the problem is the use of scanf. You need to pass the address of the storage variable not the storage variable itself.

However array bounds might quickly become a problem if you are reading long strings :)
 

TrainTrackHack

VIP Member
I believe the problem is the use of scanf. You need to pass the address of the storage variable not the storage variable itself.
I don't think that's it... they're arrays, so they're really just pointers to blocks of chars anyway (what scanf wants).
 

Troncoso

VIP Member
I don't think that's it... they're arrays, so they're really just pointers to blocks of chars anyway (what scanf wants).

Yes. Doesn't an array variable act as a pointer if you use it without brackets? Or would you still need to de-reference it? C isn't my best language. Haha.
 

TrainTrackHack

VIP Member
Doesn't an array variable act as a pointer if you use it without brackets?
Yes... not sure what you mean by brackets, though but when used by itself an array "variable" (it's really a constant) is a pointer to whatever type the array elements are.
 

Cromewell

Administrator
Staff member
Ah yes you are right, should be OK with the arrays. Is the seg fault when you press enter on some user input? Do you get the output file created?
 

neilofbodom

Member
Ok guys, I manage to get it working. Don't really know what the problem was with the code i posted here but as soon as i added this short algorithm

if (customers == NULL) {
printf("Could not open file \n");
exit(0);
}

it worked. It is simply a condition that if the pointer returns NULL it should print that there was an error. I found it in a book I own.

However, I do have another problem now. I am writing perfectly to the text file except for when the user is asked for the address. Due to the spaces between the words, the program is taking only the first word entered and ignoring the rest.

Reasoning it out, it is like a space or '\0' is the end of line at which point the program stops reading.

How can I solve this? Is there a function which can ignore the '\0' and maybe take an enter key or '\n' as the end of line instead?

Any help would be appreciated!!

Thanks for your replies!

Cheers
 

Ankur

Active Member
Use
Code:
fgets (address, 100, stdin);
Here 100 is the max character that will be read in address.
you can also use gets(address), but use it only if you are not doing any big project.
 

neilofbodom

Member
But wouldn't that be used to read from a text file?

What i need is to ask the user to input his address and the program to store that address in a text file as it was input.
 

Ankur

Active Member
But wouldn't that be used to read from a text file?

What i need is to ask the user to input his address and the program to store that address in a text file as it was input.
You can use fgets for both, getting input from user and from a file too.
The 3rd parameter "stdin" is used for getting input from the user.
If you want to get from the file then in the 3rd parameter you put the variable of the FILE datatype.
 

neilofbodom

Member
You can use fgets for both, getting input from user and from a file too.
The 3rd parameter "stdin" is used for getting input from the user.
If you want to get from the file then in the 3rd parameter you put the variable of the FILE datatype.

Ah i see, ok.
I tried it out but it is not working. This is my code:

CreateCustomer()
{

char address[30];

FILE *customers;

printf("Please enter the customer's address. \n");
fgets(address, 100, stdin);

customers = fopen("Customers.dat","a");
if (customers == NULL) {
printf("Could not open file \n");
exit(0);
}
fprintf(customers,"%s ",address);
fclose(customers);
return 0;

}
 
Top