相关疑难解决方法(0)

如何让我的程序等待键盘输入?

好的,所以当我遇到一个我以前从未遇到过的问题时,我只是做了一点练习.

#include <iostream>

using namespace std;

int main()
{
string empname = "";
int empage = 0;
char yes = 'y';

cout << "Please enter employee name:" << endl;
cin >> empname;
cin.get();
cout << "Your name is " + empname + " is this correct? (yes/no):" << endl;

if (yes)
{
    cout << "good" << endl;
}
else
{
    cout << "Please try again" << endl;
}

cout << "Please enter employee age:" << endl;
cin >> empage;
cin.get(); …
Run Code Online (Sandbox Code Playgroud)

c++ keyboard input

0
推荐指数
1
解决办法
7846
查看次数

为什么这个y/n程序没有输出

我有一个小C程序的问题.它输出一个问题(见下面的代码),我可以将输入放入(y和n),但之后没有其他任何事情发生,即使它是根据输入的输入(y或n)打印某些东西.但是,在我的问题之后没有输出任何内容,程序就会退出.这是代码:

#include <stdio.h>

int main()
{
        char string [80];
        static char y;
        static char n;
        printf( "ARE YOU SHAQIRI? [y/n]: " );
        scanf( "%s", string );
        if ("%s" == "y")
                printf("That's impossible. YOU CANNOT BE SHAQIRI YOU IDIOT");
        else if ("%s" == "n")
              printf("I thought not.");
        fflush ( stdin );
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

c string conditional stdin

0
推荐指数
1
解决办法
642
查看次数

为什么scanf不能正常工作

我想逐个输入数组中的16个字符......

#include<stdio.h>
void main(){
 int i,j;
 char a[4][4];
printf("Enter Values in array : ");
for ( i=0 ; i<=3 ; i++ )
{
for ( j=0 ; j<=3 ; j++ )
{
    printf("a[%d][%d] : ",i,j);
    scanf("%c",&a[i][j]);
}}
for ( i=0 ; i<=3 ; i++ )
{
for ( j=0 ; j<=3 ; j++ )
{
    printf("a[%d][%d] : %c\n",i,j,a[i][j]);
}}}
Run Code Online (Sandbox Code Playgroud)

而输出是

a[0][0] : q
a[0][1] : a[0][2] : w
a[0][3] : a[1][0] : e
a[1][1] : a[1][2] : r
a[1][3] : …
Run Code Online (Sandbox Code Playgroud)

c scanf multidimensional-array

0
推荐指数
1
解决办法
247
查看次数

scanf inside while while循环只工作一次

所以我有这个代码,它应该从用户获取坐标:

#include <stdio.h>
#include <time.h>

int main() {
int number;
char letter;
int points = 3;
    while(points < 8){
        printf("give me first coordinate (letter)");
        scanf("%c",&letter);
        printf("give me second coordinate (number)");
        scanf("%d",&number);
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,这应该继续从用户那里获取坐标,但是它只需要一次,然后以一种非常奇怪的方式粉碎,就像它没有任何理由跳过scanf一样.这是我的输出:

give me first coordinate (letter)a
give me second coordinate (number)1
give me first coordinate (letter)give me second coordinate (number)12
give me first coordinate (letter)give me second coordinate (number)df
give me first coordinate (letter)give me second coordinate (number)give me first coordinate (letter)give me second coordinate (number)sss
Run Code Online (Sandbox Code Playgroud)

我觉得很困惑,因为这是简单的代码,我没有slightes的想法是什么导致这一点.任何人?(如果它有任何区别,我的系统是山狮)

c scanf while-loop

0
推荐指数
1
解决办法
5075
查看次数

为什么编译器会跳过我的第二个scanf循环?

这是一个用于添加矩阵的程序,但似乎编译器在第二个嵌套for循环中跳过第二个scanf.

void input_add_matrices() {
    printf("Input number of rows for matrices: "); scanf("%d", &nrow_matrix1); getchar();
    printf("Input number of columns for matrices: "); scanf("%d", &ncolumn_matrix1); getchar();
    printf("\nInput elements for matrix 1:\n");
    for(i=0; i<nrow_matrix1; i++){
        for(j=0; j<ncolumn_matrix1; j++){
            scanf("%d", &element_matrix1[i][j]); getchar();
        }
    }
    nrow_matrix2=nrow_matrix1;
    ncolumn_matrix2=ncolumn_matrix2;
    printf("\nInput elements for matrix 2:\n");
    for(i=0; i<nrow_matrix2; i++){
        for(j=0; j<ncolumn_matrix2; j++){
            scanf("%d", &element_matrix2[i][j]); getchar();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是编译器错误还是其他什么?请帮忙.

输出是:

Input number of rows for matrices: 2
Input number of columns for matrices: 2

Input elements for matrix 1: …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
85
查看次数

为什么大小为 2 的字符数组打印的字符超过 2 个?

为什么某些等级 (Hurup Mutu)列项目打印有标记 (Angka Mutu)列项目(例如B+3,5,其中B+等级3,5标记),即使在我的 中data struct,我已经定义了等级 (Hurup Mutu) Mutu) char 数组只有 2 个字符,但最多可存储 5 个字符。

这并不是每行都会发生,所以我很难找到错误,请帮助我。

您可以在以下输出中看到它,我突出显示了错误的结果:

输出

这是我的 C 代码


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct data
{
    char nim[13];
    char nama[50];
    float uts, uas, tugas, kuis,total;
    char grade[2], angka[4], status[14];
};

void tampil();
void data(struct data[], int );
char* hurupMutu(int);
char* nilaiMutu(int);
char* status(int);

int main() {

    tampil();

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c arrays allocation scanf char

0
推荐指数
1
解决办法
117
查看次数

为什么fflush(stdin)不会删除缓冲区(stdin)

我有这样的测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char c, str[20];
    printf("Enter a character : ");
    scanf("%c", &c);
    printf("Enter a string : ");
    fflush(stdin);
    gets(str);
    printf("\n\n\nResult : %c\n%s\n", c, str);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我读过一些文章说这段代码会起作用,因为在扫描c字符后,缓冲区中仍然有'\n'字符.fflush(stdin)将清除缓冲区,因此gets()函数可以正常工作

但事实上,当我在Mac OS环境中编译并运行此代码时,fflush(stdin)什么都不做.我输入了一个字符(例如,'k'),然后它打印了k字符和'\n'字符.它假设允许我输入一个字符,一个字符串,然后打印它们.谁知道为什么?谢谢!

c macos

-1
推荐指数
1
解决办法
1900
查看次数

重新提示用户,直到他/她输入大于1的正整数值

我正在解决CS50(问题集1)即water.c.它要求用户编写一个程序,在几分钟内(以正整数形式)提示用户他或她的淋浴长度,然后打印相同数量的水瓶(作为整数).淋浴1分钟=消耗12瓶主要问题:问题是我们必须确保用户输入正数分钟,否则它会继续重新提示他返回输入/扫描声明.只要他进入,他输入长度<= 0,我可以使用while(长度<= 0)条件重新提示他,但当他输入一个字符,即输入中的abc123时,我的代码继续执行.有解决方案??

>

 #include <stdio.h>
 int main()
 {   int length=0;
    int min=12;
    int bottle=0;
    printf("Enter length of his or her shower in minutes");
    scanf("%d", &length);
    while (length <= 0){
    printf("Enter length of his or her shower in minutes");
    scanf("%d", &length);
    }
    bottle= (min*length);
    printf("%d", bottle);

    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

c validation input cs50

-2
推荐指数
1
解决办法
911
查看次数

我在c中执行此代码时计算机崩溃的原因是什么?

我正在练习C中的结构,所以我试图执行2次此代码和两次计算机崩溃.我的计算机崩溃后,我已经关闭了两次计算机.

编译器是GCC(关于Windows平台上的MinGW).代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct person {
    char name[50];
    int age;
    int document;   

};

int main(void){
    struct person p1;
    printf("Data of the  first person\n\n");
    printf("age:  ");
    fflush (stdin);
    scanf("%i",p1.age );
    printf("Document:  ");
    fflush(stdin);
    scanf("%i",p1.document);

    printf("Age is: %i and document is: %i ",p1.age,p1.document);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

真诚的,NIN.

UPDATE ....

坏消息.现在Avast说我创造了一种病毒.因此Avast删除我的exe:

在此输入图像描述

我应该报告是否为误报?

c

-4
推荐指数
1
解决办法
98
查看次数

如何在计算c中字符串的长度时不计算空格

我正在使用以下代码,它计算字符串中的空格,但我不想计算空格.

#include<stdio.h>
#include<conio.h>

void main()
{
   char*ptr,str[30];
   int size=0;
   int word=0;

   puts("enter the string");
   gets(str);
   fflush(stdin);

   for(ptr=str;*ptr!='\0',ptr++)
   {
       size++;
   }

   printf("size of string is = %d",size);
   getchar();
}
Run Code Online (Sandbox Code Playgroud)

c string

-7
推荐指数
1
解决办法
478
查看次数