我必須爲我的C編程編寫一個程序,它將字符串讀入一個typedef結構,利用它的動態數組存儲,然後計算名稱中元音和輔音的數量,元音是'a','e', '我','O','你'和'Y'。字符 - (' - ')既不是輔音也不是元音。然後我需要更新每個年齡增加3歲的數據並打印出數據。一切都必須在不同的功能。爲什麼我在C中有一個不兼容的指針類型?
- 計算在所有7名
- 必須使用的typedef結構和動態分配的陣列創建元音和輔音的量
- 功能稍後應通過加入3年每個簡檔被更新,並印有最終結果
,我需要分析的數據是這樣的:
弗雷德Flintstone38Male
巴尼Rubble36Male
威爾瑪Flintstone37Female
貝蒂Rubble36Female
卵石Flintstone4Female
巴姆 - 巴姆Rubble3Male
迪諾Flintstone2Male
這是我這麼遠,但程序不斷告訴我我有一個不兼容的指針類型,我無法弄清楚錯誤在哪裏。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct
{
char name[20];
int age[2];
char gender[6];
}CARTOON;
// Function Prototype Declarations
int printit(CARTOON *, FILE *);
int loaddata(CARTOON *, int, FILE *, FILE *);
void countVowel (char **pNames, int *vowel, int *consonants);
void printResults (int vowel, int consonants);
// Main
int main (void)
{
char input[40];
int namesIndex;
int vowel;
int consonants;
CARTOON* pNames;
FILE *tptr;
FILE *bptr;
FILE *fptr; //reading in the file "data.txt"
if ((fptr=fopen("data.txt", "r"))==NULL)
{
printf("Error opening data.txt\n");
return -1;
}
if((bptr=fopen("lab5dat.bin","wb"))==NULL)
{
printf("Error creating lab4.bin\n");
return 2;
}
pNames =(char**)calloc(8,sizeof(CARTOON));
namesIndex = 0;
while (namesIndex < 8 && fgets(input,40,fptr))
{
*(pNames + namesIndex)=(char*) calloc(strlen(input)+1, sizeof(char));//pointer to first string in array namesIndex
strcpy(*(pNames + namesIndex), input);
namesIndex++;
}
*(pNames + namesIndex)=NULL;
namesIndex = 0;
pNames=(CARTOON*) calloc(12,sizeof(CARTOON));
loaddata(pNames, namesIndex, tptr, bptr);
fclose(tptr);
fclose(bptr);
if((bptr=fopen("lab5dat.bin","rb"))==NULL)
{
printf("Error opening lab5dat.bin\n");
return 3;
}
//printit(pNames, bptr);
// Prints the items in the "lab3.dat" file.
namesIndex=0;
while (*(pNames + namesIndex))
{
printf("%s\n", *(pNames + namesIndex));
namesIndex++;
}
// Calls function countVowel
countVowel (pNames, &vowel, &consonants);
//Calls function printResults
printResults(vowel, consonants);
system("pause");
return 0;
} //end
int printit(CARTOON *pNames, FILE *bptr)
{
int num;
printf("Data from print \n\n");
//num=fread(pNames, sizeof(CARTOON), 1, bptr);
while (!(feof(bptr)))
{
num=fread(pNames, sizeof(CARTOON), 1, bptr);
printf("%s %d %s \n",pNames->name,pNames->age,pNames->gender);
}
return 0;
}
int loaddata(CARTOON *pNames, int namesIndex, FILE *tptr, FILE *bptr)
{
printf("Data from loaddata\n\n");
while (!(feof(tptr)))
{
fgets((pNames + namesIndex)->name,20,tptr);
fscanf(tptr,"%d",&pNames[namesIndex].age);
fgets((pNames + namesIndex)->gender,18,tptr);
fwrite((pNames + namesIndex),sizeof(CARTOON),1,bptr);
printf("%s\n", (pNames + namesIndex)->name);
namesIndex++;
}
return 0;
}
//function countVowel will count the number of vowels and consonants
void countVowel (char **pNames, int *vowel, int *consonants)
{
int namesIndex;
int stringIndex;
namesIndex=0;
stringIndex=0;
*vowel=0;
*consonants=0;
while(*(pNames + namesIndex))
{
stringIndex=0;
while(stringIndex<strlen(*(pNames + namesIndex)))
{
if (isalpha (pNames[namesIndex][stringIndex])) //Reads only alphabets
switch (toupper(pNames[namesIndex][stringIndex])) //makes everything capitalized.
{ case 'A': (*vowel)++ ;break; //*vowel count gets incremented by 1 whenever a vowel is found.
case 'E': (*vowel)++ ;break;
case 'I': (*vowel)++ ;break;
case 'O': (*vowel)++ ;break;
case 'U': (*vowel)++ ;break;
case 'Y': (*vowel)++ ;break;
default: (*consonants)++;break; // Everything that is not a vowel increments consonants by 1.
}
stringIndex++; // goes to the next index in the string
}
namesIndex++; //goes to the next array index, when end of string is reached
}
}
//Prints the result of the number of vowels and consonants
void printResults (int vowel, int consonants)
{
printf ("\n\nThere are %d vowels and %d consonants\n\n", vowel, consonants);
}
眼下錯誤代碼的這兩條線之間
*(pNames + namesIndex)=(char*) calloc(strlen(input)+1, sizeof(char));//pointer to first string in array namesIndex
strcpy(*(pNames + namesIndex), input);
嗯,人們通常不想通讀大量的代碼。告訴我們哪行代碼出錯會有幫助,不是嗎? – OldProgrammer
「程序不斷告訴我,我有一個不兼容的指針類型」 - 當然它也說*哪裏*? – usr2564301
不要強制轉換malloc,calloc或realloc的返回值。我不在乎你是否想要使用C++編譯器。這是C,而不是C++,而在C中,投入malloc,calloc或realloc是愚蠢的。 – Sebivor