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 511631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:14:11+00:00 2026-05-13T07:14:11+00:00

i am relatively experienced in Java coding but am new to C++. I have

  • 0

i am relatively experienced in Java coding but am new to C++.
I have written the following C++ code as solution to the USACO training problem which I have reproduced at this url

This code looks fine to me.
However it crashes on the sample test case given.
On isolating the error, I found that if the second for loop is not run for the last iteration (I mean like in the sample test case, n = 5, so I run the loop only till i = 3 instead of i = 4), then it doesn’t crash (and produces the expected output).
Maybe the error is somewhere else, I can’t detect it.

Any ideas are welcome.
Thanks in advance.
Please excuse me for the slightly unwieldy formatting of the code (this is my first forum post). The files included are stdlib.h, stdio.h and hash_map.h
`

#include <stdlib.h>
#include <stdio.h>
#include <hash_map.h>

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};


int main(int argc, char** argv) {

    FILE *fin = fopen("gift1.in", "r");
    FILE *fout = fopen("gift1.out", "w");


    hash_map<const char*, int, hash<const char*>, eqstr> table;

    int n;
    fscanf(fin,"%d",&n);
    char name[15];
    char people[10][15];

    for(int i = 0; i < n; i++){
        fscanf(fin,"%s",name);
        strcpy(people[i],name);
        table[people[i]] = 0;
    }//ifor

    for(int i = 0; i < n; i++){
        fscanf(fin,"%s",name);
        int money;
        fscanf(fin,"%d",&money);
        int friends;
        fscanf(fin,"%d",&friends);
        char fname[15];
        int amt = money/friends;
        for(int j = 0; j < friends; j++){
            fscanf(fin,"%s",fname);
            table[fname] = table[fname] + amt;
        }//jfor
        table[name] = table[name] - friends*amt;
    }//ifor

    for(int i = 0; i < n; i++)
        fprintf(fout,"%s %d\n",people[i],table[people[i]]);

    return (EXIT_SUCCESS);
}

`

  • 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-05-13T07:14:12+00:00Added an answer on May 13, 2026 at 7:14 am

    The reason it is crashing is that vick is giving 0 friends money which causes a divide by zero exception from the following line of code: int amt = money/friends;

    You should put in some special logic to handle the case when the person has 0 friends so gives $0 away.

    As was stated in the other comments, you should use some stl classes (string,iostream, etc) to help clean up the code.

    Edit: Added the input data so the question and answer would make a little more sense

    5
    dave
    laura
    owen
    vick
    amr
    dave
    200 3
    laura
    owen
    vick
    owen
    500 1
    dave
    amr
    150 2
    vick
    owen
    laura
    0 2
    amr
    vick
    vick
    0 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
I'm a reasonably experienced Java programmer but relatively new to Java2D. I'm trying to
Ok I am relatively new to Java Programming, but have previous experience in C++.
I am relatively new to Stackoverflow and Java, but I have a little experience
I am new to Cocoa programming (but relatively experienced with other UI frameworks) and
Im relatively new to the Android world but I have a quick question. I
I'm an experienced programmer, but relatively new to SQL. We're using Oracle 10 and
I am an experienced C++/Java programmer but am new to iOS. I am writing
I’m an experienced developer, but relatively new to iOS. I’m having some trouble with
Relatively new to WinForms but I'm working on a small business app. Anyway, where

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.