【问题标题】:Problem with writing a value using CIN to individual data in a struct C++使用 CIN 将值写入结构 C++ 中的单个数据的问题
【发布时间】:2022-01-24 10:29:29
【问题描述】:

好的,这就是我想要做的,我的总体目标是创建一个狙击机器人来狙击(使用的术语是)“OG 用户名”我目前在头文件中使用结构,我的原因这样做是为了减少代码重复,使程序运行更高效。我的总体目标是从网页中提取时间戳并计算运行任务的确切时间(以毫秒为单位)。

在头文件中有这样的内容:

struct TimeTilNameDrop
{
    int days;            //Integer for days
    int hours;           //Integer for hours
    int minutes;         //Integer for minutes
    int seconds;         //Integer for seconds
    int miliseconds;     //Integer for miliseconds
};

我试图以天、小时、分钟、秒为单位获取用户的输入,但我无法计算毫秒,我很感激这不准确,因为程序运行任务的时间需要几毫秒,我需要考虑到这一点。

#pragma warning(disable : 4996)
#include <iostream>
#include <ctime>
#include <time.h>
#include <NameDropData.h> //The headerfile containing the struct

using namespace std;

//Linker Decleration.
struct TimeTilNameDrop;

void Test(TimeTilNameDrop);

int TurboSnipe(Test)
{

    cout << "Please enter the days til name drop";
    cin >> days;

    cout << "Please enter the hours til name drop";
    cin >> hours;

    cout << "Please enter the minutes til name drop";
    cin >> minutes;

    cout << "Please enter the seconds til name drop";
    cin >> seconds;
}

我已经尝试查看其他教程,其中结构位于头文件中,我知道它可能会在其本地类中工作。但是,我喜欢效率的概念。任何帮助将不胜感激。

P.S 我是菜鸟,这是我的第一个项目。我知道它可能不起作用,或者我可能没有能力,但我认为这将是一个很好的项目。

哦,如果有人对任何好的 C++ 视频课程有任何建议,欢迎提出建议,我目前一直在做 “The Cherno's” C++ 系列,我刚刚了解了指针的工作原理。

欢迎提出建议 :)

【问题讨论】:

  • int TurboSnipe(Test) 属于哪里? TimeTilNameDrop 班级?
  • 根本不清楚您的实际问题/问题是什么。
  • turbo snipe 是一种方法,我试图创建一个存储在头文件中的结构实例,它也可以在 turbo snipe 方法中访问和写入,我不知道该怎么做, 然而。这是我的第一个问题,我确实尝试说清楚,如果不是,对不起。 TimeTilNameDrop 是头文件

标签: c++ performance struct time


【解决方案1】:

我假设您正在尝试根据您的描述将信息存储到结构中。我注意到您当前所做的主要问题是您从未创建结构的实例。您需要创建结构的实例以在其中存储信息。 以下是如何做到这一点的示例:

//header file where stuct is
#include "stackOverflow.h"
//linker declaration for struct
struct TimeTilNameDrop;
using namespace std;
int main() {
    //create an instance of the stuct named timeStruct
    TimeTilNameDrop timeStruct;

    cout << "Please enter the days til name drop"<<endl;
    cin >> timeStruct.days;

    cout << "Please enter the hours til name drop"<<endl;
    cin >> timeStruct.hours;

    cout << "Please enter the minutes til name drop"<<endl;
    cin >> timeStruct.minutes;

    cout << "Please enter the seconds til name drop"<<endl;
    cin >> timeStruct.seconds;

    return 0;
}

【讨论】:

  • 谢谢,伙计!解决方案已解决,感谢您对我的轻松,我知道下次我可能会更清楚。 :)
  • 没问题,我记得我刚开始的时候也很迷茫。
  • 嘿,TANK1_41 你有什么地方可以提高我的知识,比如任何视频、书籍、文档吗?任何事情都会有所帮助。 :)
  • Cherno 很不错,有一些不错的 udemy 课程,但最重要的是不仅要观看视频,还要自己编写代码并开展项目以应用所学的概念。跨度>
  • 啊,感谢您的帮助,我成功了。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多