Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:05:23+00:00 2026-06-12T23:05:23+00:00

This seems like it should be easy, which is why it is driving me

  • 0

This seems like it should be easy, which is why it is driving me especially insane. Hopefully someone out there will see the problem right off. I’m just trying to build arrays from an array built from a user input. It seems to create an array that is bigger than the one I meant for it to. Here’s the program:

    int main()
{

  ifstream inFile;
  ofstream outFile;

  int numReq, fileSize;
  string lang, dash;

  char fileName[40];
  char confirm[10];
  char confirm2[10];
  int character;
  char year[3];
  char month[1];
  char day[1];
  char hour[1];


  cout << "What file to process?" << endl;
  cin >> fileName;

  year[0] = fileName[14];
  year[1] = fileName[15];
  year[2] = fileName[16];
  year[3] = fileName[17];

  cout << "year = " << year << "." << endl;

  month[0] = fileName[18];
  month[1] = fileName[19];

  cout << "month = " << month << "." << endl;
  cout << "so I gotta know, what is..." << endl;
  cout << "month[0]? " << month[0] << endl;
  cout << "month[1]? " << month[1] << endl;
  cout << "month[2]? " << month[2] << endl;
  cout << "month[3]? " << month[3] << endl;
  cout << "month[4]? " << month[4] << endl;
  cout << "month[5]? " << month[5] << endl;
  cout << "month[6]? " << month[6] << endl;


  day[0] = fileName[20];
  day[1] = fileName[21];

      cout << "day = " << day << "." << endl;

  hour[0] = fileName[23];
  hour[1] = fileName[24];

  cout << "hour = " << hour << "." << endl;

  cout << "so, 'fileName[23]' is = " << fileName[23] << "?" << endl;
  cin >> confirm;

  cout << "So, the year is " << year << ", the month is " << month
       << ", the day is " << day << ", the hour is " << hour << "?" << endl;
  cin >> confirm;

  //cout << "Is this what you chose? " << fileName << endl;                           
  //cin >> confirm;                                                                   
  //cout << "Which character to manipulate?" << endl;                                 
  //cin >> character;                                                                 

  //cout << "This one? " << fileName[character] << endl;                              
  //cin >> confirm2;                                                                  

  inFile.open(fileName);
  assert (!inFile.fail());


  outFile.open("revisedPracticeFile1.txt");

  outFile << fixed << showpoint; // I have no idea what this is...                    
  outFile << setprecision(2);    // .. or this for that matter.                       

  cout << "Processing data" << endl;

  inFile >> lang;

  while (!inFile.eof() ){
    if (lang.length() <= 2){

      outFile << lang << " ";


      // I should keep in mind, that, for whatever reason, it seemed like the         
      //item 'setw(6)' made the program work when I put it in, but didn't seem        
      //to make the program stop working when I took it out. Curious..                

      inFile >> dash >> numReq >> fileSize;

      outFile << numReq << " " << fileSize << endl;

    }
    else{
      inFile >> dash >> numReq >> fileSize;
          cout << "took out " << lang << " " << numReq << " " << fileSize << endl;
    }

    inFile >> lang;
  }
  inFile.close();
  //assert(!inFile.fail());                                                           
  outFile.close();

  return 0;
}

…And, this is what happens when I run the program:

    What file to process?
projectcounts-20090101-010000                                      
year = 2009.
month = 01009.
so I gotta know, what is...
month[0]? 0
month[1]? 1
month[2]? 0
month[3]? 0
month[4]? 9
month[5]? 
month[6]? 
day = 011009.
hour = 0111009.
so, 'fileName[23]' is = 0?
yes
So, the year is 1009, the month is 11009, the day is 111009, the hour is 0111009?
^C

… So what gives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:05:25+00:00Added an answer on June 12, 2026 at 11:05 pm

    The syntax char year[3]; declares an array with 3 element. But, then you use it to store 4 elements. There are similar issues with your other arrays.

    Also, you’re using char arrays as strings. That’s a C (not C++) way to do things. Of course you’re allowed to do this if you want. But, these c-style strings use the convention that the last item is a zero.

    Thus, if you wanted a C-style string to store the work ‘foo’, you could do it like this

    char string[10];  // anything bigger than 3 works
    string[0] = 'f';
    string[1] = 'o';
    string[2] = 'o';
    string[3] = '\0';  // this zero tells functions like `printf` that the string has ended.
    

    Without that last zero, functions like printf will just keep outputting memory locations until it happens upon a zero somewhere.

    EDIT: Consider using c++ std::string for your string processing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be something easy, yet it is driving me mad!!
Seems like this should be an easy task but I can't figure out how
This seems like it should be easy, but I can't quite find an explanation
This seems like it should be an easy thing to do, but for the
This seems like it should be very easy...anyway, it is in MS SQL Server
This seems like it should be really easy but I couln't find an answer
This seems like it should be something very easy to do, but every time
This seems like it should be relatively straightforward, I cannot figure out what I'm
This seems like it should be obvious but I can't figure it out. Suppose
This seems like it should be easy as pie, but I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.