我是C++的新手,并且一直在学习这样的东西:
cout << "My age is: ";
cin >> age;
Run Code Online (Sandbox Code Playgroud)
和cin一起乱搞,我遇到了障碍.
说我想写"我已经x岁了!".
"x"是cin >>年龄;
我写这样的代码.
cout << "I am ";
cin >> age;
cout << "years old!";
Run Code Online (Sandbox Code Playgroud)
障碍是这会产生换行符.
我不希望换行.
我怎么能把这一切留在一条线上?
我到处寻找这个功能,找不到头文件来使这个工作.它说clrscr()未声明,这让我想到了这个问题.是clrscr(); C++中的函数?
在旧的Turbo C++编译器上编程C时,我可以使用"conio.h"头文件的clrscr()方法,但不能使用Dev C++ 5.4.2.(它给出了一个不寻常的错误ID返回1退出状态.虽然它有与clrscr()无关,当我删除clrscr()语句时,它完全正常!)因此,方法clrscr()方法已被弃用.这个错误是什么意思?
还有一个问题是"是与语言相关的编译器和库".因此对于特定的编译器,相应的库就像绑定它一样.
四处寻找执行清除 cmd.exe 控制台窗口的(据称)简单任务的最佳方法,并发现只需使用system('cls'); 是错误的(在“对于每个复杂的问题都有一个清晰、简单和错误的答案”这一行)并且发现简单地将 win32 代码复制并粘贴到函数中会导致编译错误,然后我发现了以下函数,但不知道我必须做什么才能使用它可以使用的变量来调用它,而不会发出错误消息:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
/* Standard error macro for reporting API errors */
#define PERR(bSuccess, api){ if(!(bSuccess)) printf("%s:Error %d from %s \
on line %d\n", __FILE__, GetLastError(), api, __LINE__); }
HWND GetConsoleHwnd(void) {
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
// …Run Code Online (Sandbox Code Playgroud) 我第一次在开发控制台中编译并运行我的程序时,当我打开它时,我的电脑开始滞后,它在我的桌面上创建了一堆 .tmp 文件,名称类似于trzFE47.tmp我的电脑开始滞后我不得不关闭我的电脑我什至检查过任务管理器的后台进程处理可疑的事情,但我什么也没发现,所以当我重新启动我的电脑时,我再次编译我的 .cpp 程序,avast 给了我一个警告,说在我的代码中检测到可疑项目
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;
int main() {
int l;
int a;
int b;
int c;
Sleep(3000);
srand(time(0));
l = 1+rand()%6;
a = 1+rand()%6;
b = 1+rand()%6;
c = 1+rand()%6;
cout << a << endl << b << endl << c << endl;
if (a==b&& b==c&&c==a){
int v;
cout << "you win";
cin >> v;
}else{
cout << "try again?"; …Run Code Online (Sandbox Code Playgroud)