首先,我是编程新手。我遇到的问题是,我无法覆盖此char,因为IDE告诉我,即使我没有将其声明为1,它也是一个常量。第二个问题是,它不会打印出用户名(我用注释标记了这些行),错误消息告诉我这也与常量char有关。编辑:关于2.问题,我刚刚意识到,我使用%d而不是%c,但是一旦我更改了它,那只会使情况变得更糟
这是“ PersonenDrinks.h”:
struct Person {
int Armbrust;
int Leistung;
int Kamikaze;
int Fraktion;
int Spiral;
int Zyklop;
int Glueklich;
int Waesche;
int Bildhauer;
int Neuheit;
int Matze;
int Kaenguru;
int Parfuem;
int Tasche;
int Montieren;
char Name [25];
};
这是主要代码:
#include <math.h>
#include <iostream>
#include <string>
#include <time.h>
#include "PersonenDrinks.h"
int main()
{
struct Person User1;
struct Person User2;
struct Person User3;
struct Person User4;
struct Person User5;
struct Person User6;
struct Person User7;
struct Person User8;
struct Person User9;
struct Person User10;
struct Person User11;
struct Person User12;
struct Person User13;
struct Person User14;
struct Person User15;
struct Person User16;
User1.Name= "Kevin";
User2.Name = "Stephie";
User3.Name = "Erich";
User4.Name = "TomLaurin";
User5.Name =
User6.Name =
User7.Name =
User8.Name =
User9.Name =
User10.Name =
User11.Name =
User12.Name =
User13.Name == NULL;
User14.Name == NULL;
User15.Name == NULL;
User16.Name == NULL;
for (; ; )
{
int Person;
int Getraenk;
char AddUser;
printf_s("Add User? = y/n \n");
scanf_s(" %c",&AddUser);
if (AddUser == 'y')
{
system("cls");
printf_s("Bitte geben sie ihren Vornamen ein: \n");
if (User13.Name == NULL)
{
scanf_s(" %c", &User13.Name);
if (User13.Name != NULL)
{
if (User14.Name == NULL)
{
scanf_s(" %c", &User14.Name);
if (User14.Name != NULL)
{
if (User15.Name == NULL)
{
scanf_s(" %c", &User15.Name);
if (User15.Name != NULL)
{
if (User16.Name == NULL)
scanf_s(" %c", &User16.Name);
}
}
}
}
}
}
}
else
{
printf_s("Bist du sicher, dass du dich nicht vertippt hast? Hattest du villeicht schon ein paar Drinks zu viel?");
}
printf_s(User1.Name ,"%d = 1 \n"); //here is the 2.
printf_s(User2.Name ,"%d = 2 \n"); //problem
printf_s(User3.Name ,"%d = 3 \n"); //and here
printf_s(User13.Name ,"%d = 13 \n"); //and here
scanf_s("%d", &Person);
switch (Person)
{
case 1:
system("cls");
std::cout << "Was darf's sein? \n";
std::cout << "Armbrust = 1 \n";
std::cout << "Leistung = 2 \n";
std::cout << "Kamikaze = 3 \n";
std::cout << "Fraktion = 4 \n";
std::cout << "Spiral = 5 \n";
std::cout << "Zyklop = 6 \n";
std::cout << "Gluecklich = 7 \n";
std::cout << "Waesche = 8 \n";
std::cout << "Bildhauer = 9 \n";
std::cout << "Neuheit = 10 \n";
std::cout << "Matze = 11 \n";
std::cout << "Kaenguru = 12 \n";
std::cout << "Parfuem = 13 \n";
std::cout << "Tasche = 14 \n";
std::cout << "Montieren = 15 \n";
scanf_s("%d", &Getraenk);
system("cls");
switch (Getraenk)
{
case 1:
User1.Armbrust++;
std::cout << "Das ist deine " << User1.Armbrust << ". Armbrust \n";
system("pause");
system("cls");
break;
case 2:
User1.Leistung++;
std::cout << "Das ist deine " << User1.Leistung << ". Leistung \n";
system("pause");
system("cls");
break;
case 3:
User1.Kamikaze++;
std::cout << "Das ist deine " << User1.Kamikaze << ". Kamikaze \n";
system("pause");
system("cls");
break;
case 4:
User1.Fraktion++;
std::cout << "Das ist deine " << User1.Fraktion << ". Fraktion \n";
system("pause");
system("cls");
break;
case 5:
User1.Spiral++;
std::cout << "Das ist deine " << User1.Spiral << ". Spiral \n";
system("pause");
system("cls");
break;
case 6:
User1.Zyklop++;
std::cout << "Das ist dein " << User1.Zyklop << ". Zyklop \n";
system("pause");
system("cls");
break;
case 7:
User1.Glueklich++;
std::cout << "Das ist deine " << User1.Glueklich << ". Glueklich \n";
system("pause");
system("cls");
break;
case 8:
User1.Waesche++;
std::cout << "Das ist deine " << User1.Waesche << ". Waesche \n";
system("pause");
system("cls");
break;
case 9:
User1.Bildhauer++;
std::cout << "Das ist deine " << User1.Bildhauer << ". Bildhauer \n";
system("pause");
system("cls");
break;
case 10:
User1.Neuheit++;
std::cout << "Das ist deine " << User1.Neuheit << ". Neuehieit \n";
system("pause");
system("cls");
break;
case 11:
User1.Matze++;
std::cout << "Das ist deine " << User1.Matze << ". Matze \n";
system("pause");
system("cls");
break;
case 12:
User1.Kaenguru++;
std::cout << "Das ist deine " << User1.Kaenguru << ". kaenguru \n";
system("pause");
system("cls");
break;
case 13:
User1.Parfuem++;
std::cout << "Das ist deine " << User1.Parfuem << ". Parfuem \n";
system("pause");
system("cls");
break;
case 14:
User1.Tasche++;
std::cout << "Das ist deine " << User1.Tasche << ". Tasche \n";
system("pause");
system("cls");
break;
case 15:
User1.Montieren++;
std::cout << "Das ist deine " << User1.Montieren << ". Montieren \n";
system("pause");
system("cls");
break;
break;
}
break;
}
}
}
答案 0 :(得分:1)
struct Person User1;
strcpy(User1.Name, "Kevin");
printf("%s", User1.Name);
必须工作。
当您声明char someString[50]
时,someString
是指向组成字符串的一组字符的指针,更精确地指向第一个字符。因此,您不能只做someString = "Kevin"
,而必须将“凯文”的每个字符复制到someString
中。 strcpy
是为您执行的功能。
我总是可以做类似的事情:
User1.name[0] = 'K';
User1.name[1] = 'e';
User1.name[2] = 'v';
User1.name[3] = 'i';
User1.name[4] = 'n';
User1.name[5] = '\0'
这是艰苦的工作。可以通过strcpy
完成。 '\0'
定义字符串的结尾。
答案 1 :(得分:-2)
将User1.Name= "Kevin";
替换为strcpy(User1.Name, "Kevin")
应该可以。
User13.Name
已经是一个地址,因此scanf_s(" %s", &User13.Name);
应该是scanf_s(" %24s", User13.Name, sizeof(User13.Name));
字符串的格式说明符也是%s
而不是%c
。