C ++ argv []未完全传递

时间:2014-06-03 18:46:15

标签: c++ arguments

// 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 fyi)

命令行

calender.exe 1982

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

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

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:4)

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