gpt4 book ai didi

C++ argv[] 没有被完全传递

转载 作者:可可西里 更新时间:2023-11-01 18:26:31 25 4
gpt4 key购买 nike

// Type the determine year in the command line as an argument.
// This program then prints the months and days for that year.

//Known Error argv[1] = the first digit of year,

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void printYear(int year);

int _tmain(int argc, char *argv[]){
string str;//varible used to exit the program

if (argc == 1 ){//checks to see if the years were inputted corrected
std::cout << "Please input Year in the command line. Exiting.." << std::endl;
cout << "Please type anything to continue..." << endl;
cin >> str;
return 1;
}

int Year = 1982;
int numYears = argc-1;

cout << "Number of Argments Loaded : " << numYears << endl;
for (int x = 1; x <= numYears; x++){
Year = atoi(argv[x]);
cout << "Year : " << argv[x] << endl;
cout << "Year is " << Year << endl;
printYear(Year);
}

cout << "Please type anything to continue..." << endl;
cin >> str;
return 0;
}

我目前正在学习 C++,这是我的一项作业。我只花了半天的时间研究这个问题,但无济于事。

printYear() 已经过多年测试并且可以正常使用。剩下的唯一待处理错误是 argv[]。它只返回输入年份的第一位数字,如果你想研究 0-9 年,这很好。你们介意传给我的任何提示或技巧吗? (我使用的是 Microsoft Visual Studio,仅供引用)

命令行

calender.exe 1982

返回

Number of Arguments Loaded : 1
Year : 1
Year is 1

我知道重复代码,但我正在排除故障。

最佳答案

问题是_tmain .如果您启用了 unicode,它会尝试为您提供宽 (UTF-16) 字符,因此每隔一个字符将为 \0。要解决此问题,您需要将其称为 main

关于C++ argv[] 没有被完全传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com