在 VIsual C++ 中使用 C++ 代码,没有错误,只是忽略了部分代码 [英] Using C++ code in VIsual C++, no errors but some part of the code is just ignored

查看:34
本文介绍了在 VIsual C++ 中使用 C++ 代码,没有错误,只是忽略了部分代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的绝对初学者,我只是在做一些练习练习.

I'm an absolute beginner to programming and i'm just doing some exercises exercises for the beginning.

首先,我使用 Visual C++ 2010 来编译 C 代码.我只是创建一个新项目并选择一个空的控制台应用程序.之后,我创建了一个名为 test.c 的资源文件并将文件属性中的元素类型更改为 C/C++ 编译器并编译为 C++ 代码,以便我可以使用 #include std::cin.get() 命令.现在代码:

First of all, i'm using Visual C++ 2010 to compile C-Code. I just create a new project and choose an empty console application. After that, I create a ressource file named test.c and change in the file properties the elementype to C/C++ Compiler and compile as C++ Code, so that i can use #include <iostream> for the std::cin.get() command. Now the code:

#include <stdio.h>
#include <iostream>

int main() 
{
   int number1, number2;
   int sum;

   puts("Enter number 1 please:");
      scanf_s("%d",&number1);
   puts("Enter number 2 please:");
      scanf_s("%d",&number2);

   std::cin.get();
   std::cin.get();  //(1)

   sum = number1 + number2;
      printf("The average is %f\n", sum/2);

return 0; 
}

现在我的问题是std::cin.get()"命令被忽略了.输入这两个数字后,程序会停止并关闭控制台窗口.

Now my problem ist that the "std::cin.get()" command is just ignored. Afer typing in the two numbers the program just stops and the console window closes.

知道问题出在哪里吗?

我还有一个问题.

由于我保持控制台打开的问题已解决 (1),现在我的 printf() 只给我零作为输出.我想要一个浮点数作为输出,但无论我输入什么作为 number1 和 number2,我总是得到0.000000".

Since my problem with holding the console open is solved (1), now my printf() gives me just zeros as output. I want to have a float number as output but no matter what i type in as number1 and number2 i always get "0.000000".

由于我仍在开发我的小程序以在输入被接受之前验证输入,所以我还有一个问题.

我想使用以下代码来检查输入.

I want to use the following code just to check the input.

#include <stdio.h>
#include <iostream>
#include <ctype.h>

int main() 
{
   int number1, number2;
   int sum;

   puts("Enter number 1 please:");
      scanf_s("%d",&number1);

   if (isdigit(number1)) 
   {
   puts("Enter number 2 please:");
      scanf_s("%d",&number2);
   }
   else 
   {
   puts("Your input is not correct. Enter a number please.");
   }

   std::cin.get();
   std::cin.get();

   /*



   sum = number1 + number2;
      printf("The average is %f\n", sum/2);  */

return 0; 
}

好吧,它不起作用.我输入一个数字,我的回答是您的输入不是...".我使用了搜索并发现以下内容:检查是否用户在 C 中输入一个字母或数字.不幸的是,这些建议对我没有帮助.

Well it doensn't work. I type in a digit and my response is "Your input is not...". I have used the search and found the following: Check if User Inputs a Letter or Number in C. Unfortunately the suggestions doesn't help me.

推荐答案

它不会被忽略.当您键入第二个数字,然后按 Enter 键时,它会将您的数字加上一个换行符放入输入流中.scanf 删除数字,但单独留下换行符.当您调用 cin.get() 时,由于流中有一个字符,它不会等待您的输入.

It's not ignored. When you type your second number, then hit enter, it puts your number plus a newline character in the input stream. scanf removes the number, but leaves the newline character alone. When you call cin.get(), since there's a character in the stream, it doesn't wait for your input.

这篇关于在 VIsual C++ 中使用 C++ 代码,没有错误,只是忽略了部分代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆