首页 > 解决方案 > 在 C 中无法识别字符串

问题描述

这是我的代码,在我使用字符串时,它表示未知类型名称。我试图包括但仍然没有工作。我不明白这里发生了什么。任何帮助将不胜感激。

#include<stdio.h>
#include<conio.h>
string  getname(string);
int getclass(int);
float calculation(int);
int main()
{
    string str2=getname();
    printf("name of student is %s \n",str2);
    int b=getclass(b);
    printf("class = %d \n",b);
//  float per=calculation(/*marks kithy ny??*/);
//  printf("percentage = %f \n",per);
    getch();
    return 0;

}
string getname(string str /* str likhdy nay ithy variable da nam sirf is nal coma nhi landay */)
{
    printf("enter the name of student \n");
    scanf("%s",&str);
    return str;
}
int getclass(int a)
{
    printf("enter the class \n");
    scanf("%d",&a);
    return a;
}
float calculation(int marks)
{
    printf("enter the marks \n");
    scanf("%d",&marks);
    int per=marks*100/550;
    return per;
}

标签: c

解决方案


string不是C类型。string用替换函数参数char *

char* getname(char*);

由于在 this 中作为指针传递的数组C将起作用。并使用 char 数组来存储字符串值。

char str2[10];
getname(str2);

最后一件事,不要&在 scanf 中将运算符与数组或指针一起使用。利用:

scanf("%s", str);

推荐阅读