我正在為 c 中的特定產品創建一個銷售專案。發生的情況是每個客戶都需要注冊一個客戶編號,但我有一個問題,因為程式管理該編號,但是當它把它保存在txt檔案中時,它沒有給出正確的編號。但是,我對客戶的姓名還有另一個問題,如果我寫全名,則會出現錯誤。我該如何解決這些問題?謝謝
這是我的代碼:
#include <stdlib.h>
#include <stdio.h>
typedef struct client CLIENT;
struct client {
char name[80];
char country[10];
int tin;
};
void createprofile() {
char tin_present;
int i;
FILE* bd;
bd = fopen("bd.txt", "a ");
CLIENT c;
printf("\t *** Create customer profile *** \n");
printf("Client code: ");
for (i = 0; i < 1; i ) {
printf("%d\n ", rand() % 1000000);
}
printf("Type your name: ");
scanf("%s", &c.name);
printf("TIN present?? ");
scanf("%s", &tin_present);
if (tin_present == 's' || tin_present == 'S') {
while (1) {
printf("\n\tEnter your TIN: ");
scanf("%d", &c.tin);
if (c.tin >= 999999999) {
printf("\tNumber with more than 9 digits is not allowed. Please try again.");
} else if (c.tin <= 99999999) {
printf("\tNumber with less than 9 digits is not allowed. Please try again.");
} else
break;
}
printf("\tValid.\n");
} else {
printf("TIN not entered\n");
}
printf("What is your country? ");
scanf("%s", &c.country);
fprintf(bd, "code: %d | name: %s | tin: %i | country: %s \n", c.name, c.tin, c.country);
fclose(bd);
}
uj5u.com熱心網友回復:
- 發生的情況是每個客戶都需要注冊一個客戶編號,但我有一個問題,因為程式管理該編號,但是當它把它保存在txt檔案中時,它沒有給出正確的編號。
在這種情況下,正如ryyker已經評論過的,問題在于您沒有將創建的數字存盤在任何地方,您只是在列印它。因此,您的程式只是在創建一個永遠不會被讀取或使用的值。
您可以按照ryyker 的建議進行操作:
- 在您的 CLIENT 物件中創建一個新的“資訊”,您可以在其中存盤 client-id 的值(您的亂數);
所以,考慮改變這個:
typedef struct client CLIENT;
struct client {
char name[80];
char country[10];
int tin;
};
對此:
/* If you want to, you can do this, change your struct prototype
and condense it into the struct declaration */
typedef struct client {
char name[80];
char country[10];
int tin;
int client_code; // or any other variable name your prefer to
} CLIENT; // and condensing, you write here the struct shortcut/typedef-name
- 現在,您也可以將生成的值存盤在您的客戶端資訊中,因此:
for(i = 0; i < 1; i ) {
printf("%d", rand() % 1000000);
}
應該轉換成這個(注意,正如之前已經評論過的,沒有必要for只用一次迭代來使用它,然后把它撕掉):
c.client_code = rand() % 1000000;
- 然后將新值添加到您的
bd.txt檔案中:
fprintf(bd, "code: %d | name: %s | tin: %i | country: %s \n", c.client_code, c.name, c.tin, c.country);
- 我對客戶的名字有另一個問題,如果我寫全名,它會出錯。
我猜您遇到的問題可能出在scanf函式上。在 C 中,向量被視為指標。話雖如此,當您在程式的某個位置寫入向量名稱時,程式收到的是向量第一個位置的記憶體地址。
Achar是只需要一個字符的資料型別,所以當我們想在 C 中使用字串時,我們需要創建一個字符陣列,這樣我們就可以創建包含多個字符的單詞。
scanf當我們想要讀取一個字串時,該函式需要一個指標,然后如果一個字串是一個字符向量并且一個向量將一個記憶體地址傳遞給函式/程式,我們將不會使用&in scanf,因為這個符號意味著傳遞記憶體函式的變數地址。
考慮進行此更改:
printf("Type your name: ");
scanf("%s", &c.name);
為此(這同樣適用于您的國家掃描):
printf("Type your name: ");
scanf("%s", c.name); // without the '&'
OBS:如果您想讀取帶空格的字串/全名,您應該使用以下格式scanf:
printf("Type your name: ");
scanf("%[^\n]s", c.name); // [^\n] tells to the program to ignore newline characters until "Enter" be pressed
uj5u.com熱心網友回復:
將客戶端代碼成員添加到結構體,即 int client_code;
然后考慮改變這個:
for (i = 0; i < 1; i ) {
printf("%d\n ", rand() % 1000000);
}
對此:
c.client_code = rand() % 1000000;
printf("Client Code: %d\n ", c.client_code);
使用注意事項rand()。如果尚未完成,(在您未顯示的代碼中)應在程式早期呼叫srand(),僅在呼叫之前呼叫一次rand(),以確保您的偽隨機值是真正的偽隨機值。(該鏈接提供了如何操作的示例。)
然后在您的檔案輸出中,添加客戶端代碼:
fprintf(bd, "code: %d | name: %s | tin: %i | country: %s \n", c.client_code, c.name, c.tin, c.country);
其他建議可能包括使用結構陣列來包含多個客戶端
#define MAX_CUSTOMERS 100 //or what makes sense for your need
typedef struct {
char name[80];
char country[10];
int tin;
int client_code;
}CLIENT_s;
CLIENT_s client[MAX_CUSTOMERS];
...或使用鏈表在其中讀取您的客戶端檔案,添加、洗掉客戶端,然后更新檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383274.html
標籤:C
