C ++ argv [] not fully passed

// 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; } 

I am currently learning C ++, and this is one of my assignments. I just spent the better half of the day looking at it to no avail.

printYear () has been tested over many years and is functional. The only residual error is argv []. It returns only the first digit of the entered year, which is great if you want to research years 0-9. Any tips or tricks that you guys miss me? (I am using Microsoft Visual Studio fyi)

Command line

 calender.exe 1982 

returns using

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

Duplicate code that I know, but I have problems.

+7
c ++ arguments
source share
2 answers

_tmain problem. If you enable Unicode, it tries to provide you with wide (UTF-16) characters, so every other character will be \0 . To fix this, you want to call it main instead.

+7
source share

The arguments seem to be passed as UNICODE strings, however you treat them in the program as ASCII strings.

+4
source share

All Articles