c - Newlines being outputted actually as "\n"
问题描述
This is my code so far:
#include<stdio.h>
#include <string.h>
int main(void) {
char userString[500];
int x = 0;
int y = 0;
while(x < 1 && (!(userString[0] == 'q'))){
printf("Enter input string:\n");
fgets(userString,500,stdin);
for(int i = 0; i < strlen(userString);++i){
if(userString[i] == ','){
++x;
}
}
if(x < 1){
printf("Error: No comma in string.");
printf("\n");
}
}
This is what is being outputted:
b'Enter input string:\nError: No comma in string.\nEnter input string:\nError: No comma in string.\nEnter input string:\nError: No comma in string.\nEnter input string:
This is the desired output:
Enter input string:
Error: No comma in string.
Enter input string:
Error: No comma in string.
Enter input string:
Error: No comma in string.
Enter input string:
The goal of the program is to get a string with a comma as input, and if there is no comma in the string, to keep prompting the user until they enter "q".I have not even the slightest clue why this could be happening. Would appreciate advice.
解决方案
推荐阅读
- python - 为什么熊猫操作 df.loc[:, ['a', 'b'] = df.loc[:, ['c', 'd'] 不会改变 df.loc[:, ['a ','b']?
- ruby-on-rails - 如何在 Rails 中将变量从控制器传递到视图,再到控制器?
- php - 如何使用 laravel 5.8 将文件存储到网络共享文件夹中
- sql - 使用 AND 运算符时 Postgres 查询不获取数据
- python - 试图将英寸转换为英尺并收到错误:- TypeError: 'float' object is not subscriptable
- scala - Scala将可变参数传递给另一个接受可变参数的函数
- c# - Sentinel HASP 为 64 位 DLL 返回 NoApiDylib
- windows-10-iot-core - Windows IoT Core 恢复 - 管理员密码
- c# - 为什么程序指向错误的路径?
- r - 将参数传递给函数内的子集