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

C++ argv[] not being passed fully

本文关键字:argv c++      更新时间:2023-10-16
// 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)字符,因此每个其他字符将是。要解决这个问题,你需要将其命名为main

似乎参数是作为UNICODE字符串传递的,但是您在程序中将它们作为ASCII字符串处理。