So I have a list of names and corresponding phone numbers, and I want the user to be able to continuously enter a new name-number pair into that list. The part of my code where I try to do that looks something like this:
char name[20], list_names[1000][20], phone[20], list_phone[1000][20];
int n;
n = 0;
do
{
printf("Enter name: ");
scanf("%20[^\n]", name);
printf("Enter phone number of %s: ", name);
scanf("%20[^\n]", phone);
strcpy(list_names[n], name);
strcpy(list_phone[n], phone);
n += 1;
}
while (n < 1000);
This usually gives me an error like "incompatible pointer type". I have to do it the indirect way and first store the name in a separate variable. But how do I get the string from that variable into the list? Probably there's something I don't get in the strcpy() part.
Thanks for helping out!
try this
printf("Enter name: ");
scanf(" %19[^\n]", name);//add one space and turn 20 to 19 (leave space for '\0')
printf("Enter phone number of %s: ", name);
scanf(" %19[^\n]", phone);
Related
I have homework in my C programming class.
I have to input 3 people's information about name, major, ID(ex. 9304171)and output to console
'name, birth(yyyy-mm-dd), leep year, nationality, sex, major'.
I tried to that, but my scanf_s wasn't activated.
if i try to activate to my code, char array can activated, but my integer array can't activated.
char name[4]; // name
char major[4]; // major
int id[7] = {}; // ID
// Input name, major, ID
printf("Name : ");
scanf_s("%s \n", &name, 4);
printf("Major : ");
scanf_s(" %s \n", &department, 4);
printf("ID : ");
scanf_s("%d \n", id);
My English ability is so fool... but it is make me so angry, i begging to help..
plz, sombody help me T.T
Declare something like:
char name[50], major[50];
int id; // array removed
You can use fgets() if you're having issues with scanf_s(), I suspect you've given such short number of elements for name array (i.e. 4) and that's something relatable with the error. Try the following using fgets():
printf("Name: ");
fgets(name, 50, stdin); // fgets buffered for 50 elements
printf("Major: ");
fgets(major, 50, stdin); // fgets ...
printf("ID: ");
scanf("%d", &id); // scanf() can be used here
Let's take a look at a sample output (no errors):
// INPUT
Name: John Doe
Major: Something
ID: 101
// OUTPUT
Name: John Doe
Major: Something
ID: 101
#include <stdio.h>
#include<stdlib.h>
int main()
{
char first[10], last[10], id[10];
int stdnum;
FILE *fptr;
fptr = fopen("C:\\c\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
else
{
//first name
printf("Enter name: ");
scanf("%s", first);
fprintf(fptr,"%s ",first);
//last name
printf("Enter last name: ");
scanf("%s ", last);
fprintf(fptr,"%s", last);
//id
printf("Enter id: ");
scanf("%s %d", id, &stdnum);
fprintf(fptr,"%s\n", id);
fprintf(fptr,"%d\n", stdnum);
fclose(fptr);
return 0;
}
}
I am writing a program that asks user for name, last name and student number. Student number format is lowercase letter followed by 8 digits.
when i run this code, I can enter first and last name. After i enter last name and hit enter in cygwin, the console gives me a blank line and I MUST type atleast a charcter or a number into it for it to display "enter student id".
Enter name: bob
Enter last name: jones
1
Enter id: a00998877
then the file output is something like this:
"bob jones11"
I want the output to be something like this:
"Bob Jones a00998877"
What am i doing wrong?
Why are you scanning in two inputs in this line:
scanf("%s %d", id, &stdnum);
id is a char array and will be able to hold alphanumeric input. You can remove the use of stdnum.
scanf("%s", id);
I have an example-with-stdin-stdout
char first[10], last[10], id;
id is a single char no need for an array.
scanf("%s", last);
Removed space after s
scanf(" %c%d", %id, &stdnum)
changed id to char from string
fprintf(fptr,"%c\n", id)
Same here, changed to char
Yes ,I know that this question was already asked for many times ,but none of these helped me to discover the problem (duplicate...yeah). I want to read from input a series of strings into an array and then search from 'First Name'. If the name exist ,I want to display all the data stored in that element of array (I attached the code to undestand easily). When I run it ,I read from keyboard all the data ,but it returns me absolutely nothing.
#include<stdio.h>
typedef struct record {
char name[10],lname[10],phone[10],bday[10];
};
void main() {
struct record rec;
char search;
int i,nr;
printf("\nInput number of records: ");
scanf("%d",&nr);
for (i=0 ; i<nr ;i++) {
printf("First name: ");
scanf("%s",&rec.name[i]);
printf("Last name: ");
scanf("%s",&rec.lname[i]);
printf("Phone: ");
scanf("%s",&rec.phone[i]);
printf("Bday: ");
scanf("%s",&rec.bday[i]);
}
printf("Input the first name for searching: ");
scanf("%s",&search);
for (i=0 ;i<nr;i++) {
if (search == rec.name[i]) {
printf("First name: %s\nLast name: %s\nPhone: %s\nB-day: %s",rec.name[i],rec.lname[i],rec.phone[i],rec.bday[i]);
}
}
}
NOTE: I already replaced
scanf("%s",&rec.name[i]);
with
scanf("%s",rec.name[i]);
but no effect.
I believe there are a lot of problems with your code.
Firstly in this line:
scanf("%s",&search);
You have declared search as only a char, when really you want an array of chars. You also don't need & with search, as an array decays to a pointer to the first element.
It instead should be like this:
char search[10];
scanf("%9s", search); /* %9s to avoid buffer overflow */
You need to make this change to all your other scanf() calls, as this seems to be everywhere in this code.
It also seems that you want to create an array of records(structures), So you might need to make this after getting the value of nr. You can create it like this:
struct record rec[nr]; /* array of nr structures */
This also means calls like this:
rec.name[i]
Don't make sense, as you are iterating over the characters within a name, not over all the records in struct records.
This needs to be instead:
rec[i].name
Secondly, Your using == to compare strings, when you should be using strcmp instead. Using == will only compare the base address of the strings, not the actual contents of strings.
Your line should be this instead:
if (strcmp(search, rec[i].name) == 0) {
If you read the manual page for strcmp(), checking for a return value of 0 means that both strings are equal in comparison.
Lastly, in your first scanf() call:
scanf("%d",&nr);
You should really check the return value of this:
if (scanf("%d", &nr) != 1) {
/* exit program */
}
Note: For reading strings, you should really be using fgets instead. You can try upgrading to this later, but I think it is better to understand these basics first.
Here is working example of what your program should do:
#include <stdio.h>
#include <string.h>
#define STRSIZE 10
typedef struct {
char name[STRSIZE+1]; /* +1 to account for null-btye at the end */
char lname[STRSIZE+1];
char phone[STRSIZE+1];
char bday[STRSIZE+1];
} record;
int main() {
char search[STRSIZE+1];
int i,nr;
printf("\nInput number of records: ");
if (scanf("%d", &nr) != 1) {
printf("Invalid input.\n");
return 1;
}
record rec[nr]; /* array of records */
for (i = 0; i < nr ; i++) {
printf("First name: ");
scanf("%10s", rec[i].name);
printf("Last name: ");
scanf("%10s", rec[i].lname);
printf("Phone: ");
scanf("%10s", rec[i].phone);
printf("Bday: ");
scanf("%10s", rec[i].bday);
}
printf("Input the first name for searching: ");
scanf("%10s", search);
for (i = 0; i < nr; i++) {
if (strcmp(search, rec[i].name) == 0) {
printf("First name: %s\nLast name: %s\nPhone: %s\nB-day: %s\n",rec[i].name,rec[i].lname,rec[i].phone,rec[i].bday);
} else {
printf("Record not found.\n");
}
}
return 0;
}
The numeric input leaves a new line character in the input buffer, which is then picked up by the character input. when numeric input with scanf() skips leading white space, character input does not skip this leading white space.
Use a space before %c and it will help you cause if space is not used then a buffer added with value .so that use space before %c
scanf(" %c",&rec.name[i]);
I have a looped struct array that accepts student name and roll number for as many students the user inputs
Code:
int x;
struct studs
{
char name[50];
char rollno[50];
}student[50];
printf("\t\t________________________________");
printf("\n\n\t\t\tStudent Registration");
printf("\n\t\t_______________________________");
printf("\n\n\tHow many students in %s ", course);
scanf("%d", &stud);
admin = fopen(name, "a");
fprintf(admin, "\nNumber of students for %s: %d", course, stud);
fclose(admin);
for(x=0; x<stud; x++)
{
printf("\n\nStudent %d", x+1);
printf("\n_____________________________");
printf("\n\nStudent Name : ");
scanf("%s", student[x].name);
printf("\nStudent Rollno. : ");
scanf("%s", student[x].rollno);
system("\npause");
goto exam;
}
admin = fopen("student.txt", "w");
fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student.name, student.rollno);
fclose(admin);
As you can see i'm trying to put each name and number into the text file but I keep getting this errors
error: request for member 'name' in something not a structure or union |
error: request for member 'rollno' in something not a structure or union
Is there anyway to get this into a file, with or without the struct?
This
fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student.name, student.rollno);
cannot work, student is an array of stud's (or at least it would be if i were in the school). You have already shown that you know how to iterate over the number of students with for(x=0; x<stud; x++), so just do it again:
for(x=0; x<stud; x++) {
fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student[x].name, student[x].rollno);
}
There are likely going to be other errors in your program (Like that goto exam immediately jumping out of your for loop to somewhere). But this will at least deal with the error you have described.
This program that I am doing for class to create an EMPLOYEE records file. I created two structs. One called employee and one called date. The EMPLOYEE struct has one char array, one int, 6 float values and DATE(another Struct). The DATE struct has just three int values (month, day, year).
I have created an array of type EMPLOYEE called person[1000].
Here is my code, I keep getting a debug assertion failed error in visual studios pointing to fwrite.c Expression: (Stream !=NULL). I am sure it has to do with my fread or fwite as I have never tried to store structs.
The point of the beginning of this function is to populate the array if the file exists and is not empty, so when the user starts storing data to the array the subscript is updated. I know there are probably a few other issues here but first things first and that would be the reading and writing portion.
Thanks Again,
Mike
void loadPayRoll(EMPLOYEE person[], int *i)
{
char sValidate[5] = "exit";
FILE *f;
int count = 0;
f = fopen("emplyeeRecords.bin", "rb");
if(f){
while(fread(&person[*i], sizeof(person), 1, f) > 0){
(*i)++;
}
fclose(f);
}
else {
while (strcmp( sValidate, person[*i].name)) {
fopen("employeeRecords.bin", "ab+");
printf("Please enter name or type exit to return to main menu: ");
scanf("%s", person[*i].name); //must use the '->' when passing by by refrence, must use '&' sign
flush;
if (!strcmp( sValidate, person[*i].name))
break;
printf("\nPlease enter age of %s: ", person[*i].name);
scanf("%i", &person[*i].age);
flush;
printf("\nPlease enter the hourlyWage for %s: ", person[*i].name);
scanf("%f", &person[*i].hourlyWage);
flush;
printf("\nPlease enter the hours worked for %s: ", person[*i].name);
scanf("%f", &person[*i].hoursWkd);
if (person[*i].hoursWkd > 40) {
person[*i].regPay = person[*i].hoursWkd * 40;
person[*i].otHoursWkd = person[*i].hoursWkd - 40;
person[*i].otPay = person[*i].otHoursWkd * (person[*i].hourlyWage * 1.5);
person[*i].totalPay = person[*i].regPay + person[*i].otPay;
}
else {
person[*i].totalPay = person[*i].hoursWkd * person[*i].hourlyWage;
}
flush;
printf("\nEnter 2 digit month: ");
scanf("%i", &person[*i].payDate.month); //must use the '->' when passing by by refrence, must use '&' sign
flush;
printf("\nEnter 2 digit day: ");
scanf("%i", &person[*i].payDate.day); //must use the '->' when passing by by refrence, must use '&' sign
flush;
printf("\nEnter 4 digit year: ");
scanf("%i", &person[*i].payDate.year); //must use the '->' when passing by by refrence, must use '&' sign
flush;
fwrite(&person[*i], sizeof(person), 1, f);
fclose(f);
(*i)++;
}
}
}//end function loadPayRoll
I'm pretty sure this:
fopen("employeeRecords.bin", "ab+");
sitting all by its lonesome on a single line without assigning the resulting FILE* has quite a bit to do with your problem.There are plenty of other issues for example:
flush;
Not really sure what thats all about. Perhaps you meant:
fflush(f);
Assuming f is ever actually assigned correctly,
And as pointed out in-comment, you should open the file before the loop starts, then write the data as needed, the close it after the loop is finished.