可能重复:
如何正确比较C中的字符串?
#include <iostream>
using namespace std;
int main(){
char name[100];
cout<<"Enter: ";
cin>>name;
if(name == "hello"){
cout<<"Yes it works!";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么当我在提示中输入你好时我没有得到"是的,它有效!" 信息?
我有一个C++控制台应用程序代码,它将查找条件,然后将文本保存到(.txt)文件.
但这里没有适用这个条件.我的条件是:
char command[100];
cin >> command;
if (command == "save") {
fstream file;
file.open("C:\\Users\\AfzaalAhmad\\Documents\\text.txt");
file << "Data you provided was as saved!";
cout << "File Saved!";
}
else {
cout << "Ummm, sir I think there is an error!\n" <<
"The command you entered was: " << command;
}
Run Code Online (Sandbox Code Playgroud)
我正在做的是,检查用户提供的命令,如果命令是'save'
if(command == "save")
Run Code Online (Sandbox Code Playgroud)
然后将数据保存到文件中,当我使用此代码时,在文件中存在数据:
if(command != "save")
Run Code Online (Sandbox Code Playgroud)
因为命令不必保存,代码执行并向我提供文档文件夹中的文件中的数据.
但是,如果else块被执行,我仍然可以在末尾看到正确的命令'save',正如您在代码块中看到的那样,代码显示了该命令.

你可以看到那里,我正在使用正确的命令,但它没有执行,if(command == "save")但如果我使用此代码它会执行if(command != "save").
有什么指导吗?
我必须创建一个程序,其中输入必须是2个值之一,'M'或'F'.
这是我创建的用于演示的程序段
do
{
printf("What is your Gender? (M for Male - F for Female) ");
fgets(GenderValue, 16, stdin);
if (GenderValue[0] == '\n')
{
printf("Try again");
getch();
system("cls");
loop=1;
}
else
{
loop = -1;
}
if (GenderValue == "M");
{
loop =-1;
}
else if(GenderValue == "F");
{
loop=-1;
}
else
{
printf("Try again");
getch();
system("cls");
loop=1;
}
}
while(loop > 0); //Checks for NULL input
printf("Gender: %s",GenderValue);
Run Code Online (Sandbox Code Playgroud)
我知道我可以做一个整数选择输入,但如果可能的话我想稍后回收它.
到目前为止,该程序调用我的性别值比较"错误",这很好,除了我不知道下一步该做什么,我可以找到一个比较字符串的函数,但我真的不想让我的代码复杂化.
编辑:复制?,真的吗?,给出的例子甚至没有接近帮助我.
编辑2:发现问题,我有;在我的if语句旁边,现在已修复.但说真的,没有人发现这个?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char username[12] = "admin";
char password[12] = "admin";
system("color 9f");
system("cls");
login:
printf(" ---------------------------------- \n");
printf(" | |\n");
printf(" | WELCOME TO OMAR WATER'S |\n");
printf(" | INVENTORY |\n");
printf(" | SYSTEM |\n");
printf(" | |\n");
printf(" ---------------------------------- \n\n\n");
printf("Please Log-In...\n\n");
printf("Username: ");
scanf("%c", &username);
if (username == "admin"){
printf("\nPassword: ");
scanf("%c", &password);
if (password == "admin"){
printf("\n\nWELCOME ", &username);
}else{
printf("INVALID PASSWORD");
getchar();
system("cls");
goto login;
}
}else { …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查指针是否指向某个字符.
像这样:
#include<stdio.h>
#include<string.h>
#define a 3
int func(char *);
int main()
{
char *s="a";
int type;
type=func(s);
printf("%d",type);
return 0;
}
int func(char *s)
{
int type;
if(*s=="a")
{
type=1;
}
return type;
}
Run Code Online (Sandbox Code Playgroud)
但我经常收到警告:警告:指针与整数之间的比较if(*s =="a")
是否可以比较指针和整数?
还有另一种方法可以解决这个问题吗?
我可以找到哪个字母指向*s而不打印它?
我正在尝试使用数组指针在c中编写程序...
我的目标是在标头值相同时打印错误消息.将通过命令行参数获取标头值.
如果我得到相同的标题值,即)(GET和GET)或(HEAD和HEAD),它应该打印有效,而对于所有其他情况,它应该打印无效.
我写的程序打印无效的所有组合.我不明白我在哪里弄错了.
int main(int argc,char *argv[])
{
char *str1[4] = { "GET","HEAD","POST","OPTIONS"};
char *str2[2] = {NULL};
char *string = argv[1];
const char s[2] = ",";
char *token = "";
int i = 0,j = 0,k = 0,l = 0,m = 0;
token = strtok(string, s);
while( token != NULL )
{
if(i < 2)
{
str2[i] = token;
//printf( " %s\n", token );
printf("str2[%d]= %s\n",i,str2[i]);
}
i++;
token = strtok(NULL, s);
}
for(l = 0;l < 4;l++)
printf("str1[%d] …Run Code Online (Sandbox Code Playgroud) 我刚刚创建了这个函数,通过减去它们的Integer值来检查两个字符串之间的相等性
int EQ(const char* CString1, const char* CString2)
{
return CString1 - CString2 == 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定这是否是检查char*字符串的安全方法
它不适用于数组,因为它们有不同的整数,所以我为此创建了其他函数
int EQA(const char* CString1, const char* CString2){
if(CString1 == NULL || CString2 == NULL)
return 0;
int i = strlen(CString1);
if(i != strlen(CString2))
return 0;
while(i>=0){
if(CString1[i] != CString2[i])
return 0;
--i;
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我没有尝试过多地优化我的第二个功能
但是在检查时char*,我为什么要使用EQA()或strcmp()结束EQ()?
第一个EQ()是非常快,比我以前使用的任何其他标准功能
所以,我真的很喜欢使用它
我在其他机器上测试它并且它工作但整数值char* 不同,也 …
我将一个参数传递给我的程序有一个问题,似乎不等于我作为参数放入的内容,除非它们是相同的.将它们变成一个字符串使它们相同,但我想知道为什么最初的二重奏不是.
这是我的代码:
int main(int argc, char *argv[]) {
if (argc>1) {
cout << "#" << argv[1] << "#" << endl;
cout << "#" << "nomast" << "#" << endl;
cout << (argv[1] == "nomast" ? "equal" : "not equal") << endl;
string s1 = argv[1];
string s2 = "nomast";
cout << (s1 == s2 ? "equal after all" : "nope") << endl;
system("pause");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用"call thingy.exe nomast"启动已编译的代码时,我得到了输出
#nomast#
#nomast#
not equal
equal after all
Press any key …Run Code Online (Sandbox Code Playgroud) 我在这里制作了一个脚本,我在do-while循环中遇到了麻烦.我可以让它无限运行(继续)或只运行一次(休息).也许我设定的条件有问题......我在这里不知所措,所以任何帮助都会非常感激
#include <stdio.h>
#include <time.h>
int main ()
{
int age;
char string[50];
char string2[4];
int yearsleft;
/*
FILE *fp;
fp = fopen( "RetireeFileForCo.txt", "a+" );
*/
do {
printf( "What is your name? \n");
scanf( "%s", string );
printf( "Hello %s\n", string );
printf ("What is your age? \n");
scanf ("%d", &age);
printf ("You enter %d\n", age );
if ( age > 65 ) {
printf (".... You should already be retired %s\n", string );
// fputs ( ".... …Run Code Online (Sandbox Code Playgroud) 我正在研究C中的国际象棋编程和UCI命令的基本使用,我试图将Silvestro Fantacci C++ HelloWorld国际象棋引擎转换为C但没有成功.
参见Silvestro Fantacci C++ HelloWorld国际象棋引擎
以下是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define BUFFERSIZE 50
int main(int argc, char **argv)
{
char line[BUFFERSIZE];
while(fgets(line, BUFFERSIZE, stdin) != NULL)
{
if (line == "uci")
{
printf("id name rpdHWchessengineinC\n");
printf("id author Richard\n");
printf("uciok\n");
}
else if (line == "isready")
printf("readyok\n");
else if (line == "ucinewgame")
; // nothing to do
else if (line == "position startpos moves e2e4\n")
; // nothing to do
else if (line …Run Code Online (Sandbox Code Playgroud) 我试图比较用户输入是否与文本文件中的相同。在printf("U = %s, R = %s.", uAts, rline);行我得到A = A,但它仍然去else语句。也许还有其他一些我不知道的 C 语言中比较字符串的方法?
void nextQ(int klL, FILE *kl, FILE *ats, FILE *atsR){
int i, uAts[500], at;
char kline[500], aline[500], rline[500], b[1];
system("cls");
for (i = 0; i <= klL; i++){
fgets(kline, 500, kl);
fgets(aline, 500, ats);
fgets(rline, 500, atsR);
}
printf("%s\n", kline);
printf("%s\n\n", aline);
printf(rline);
printf("Jusu atsakymas: ");
scanf("%s", &uAts);
printf("U = %s, R = %s.", uAts, rline);
if(uAts == rline){
printf("Klausimas atsakytas teisingai!\n");
printf("Noredami pereiti prie kito klausimo …Run Code Online (Sandbox Code Playgroud) char str[6];
do
{
printf("Enter the string you wanna check:");
scanf("%s", str);
}
while(str != "exit");
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
我是编程的初学者.我在C#中创建了一个简单的程序,但它无法正常工作.当我键入"rezistenta"时,它应该运行条件当我输入"capacity"时,
if (valoare=="rezistenta")
它应运行第二个if:
if(valoare=="capacitate")
在两种情况下程序运行最后一个,如果条件,它会跳过前两个.
该程序:
#define _CRT_SECURE_NO_WARNINGS //directive preprocesor
#include<stdio.h>
#include<conio.h>
void main(void)
{
char valoare[100];
float C1, C2, CS, CP;
float R1, R2, Rs, Rp;
printf("\nCapacitate sau Rezistenta? ");
scanf("%s", &valoare);
printf("\nAti introdus= %s", valoare);
if (valoare == "rezistenta")
{
printf("\nIntroduceti valorile rezistentelor: ");
scanf("%f%f", &R1, &R2);
Rs = R1 + R2;
printf("\nRezistenta echivalenta serie este: *%6.3f*", Rs);
Rp = (R1*R2) / (R1 + R2);
printf("\nRezistenta echivalenta paralel: *%6.3f*", Rp);
}
else if (valoare == "capacitate")
{ …Run Code Online (Sandbox Code Playgroud)