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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:41:16+00:00 2026-06-14T20:41:16+00:00

I am working on a school assignment and one part of it has me

  • 0

I am working on a school assignment and one part of it has me a little stuck. I need to have the user enter as many fractions as desired until a 0 is entered for the numerator, and save those fractions in a structure I created.

I was attempting to us strtok to split the c-string that I would use to store the user input by space and then by “/”, but I got very stuck and couldn’t get any proper output (the code below is main() it has a run time error and the second for loop would obviously fail in assigning anything proper to den).

What would be the simplest way in achieving this.

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct Fraction 
{
    int num, den;
};
int main()
{
    Fraction* fractions[100];
    char s[100] ;
    cout << "Enter fractions (end by entering a 0): ";
    cin >> s;
    const char* p;
    int count = 0;

    for (p = strtok( s, " " );  p;  p = strtok( NULL, "," ))
    {
        const char* frac = p;
        for (frac = strtok( s, " " );  frac;  frac = strtok( NULL, "/" ))
        {
            fractions[count]->num = (int)frac;
        count++;    
        }
    }
        return 0;
}
  • 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-14T20:41:17+00:00Added an answer on June 14, 2026 at 8:41 pm

    You don’t want

    Fraction* fractions[100];
    

    you just want

    Fraction fractions[100];
    

    The first is an array of pointers to Fractionss, the second is just an array of Fractions. The problem you are probably having is that you didn’t initialize the pointers.

    An even better approach might be to use a std::vector<Fraction> to store your Fractions. Then, you can dynamically add to it, and it can store as many Fractions as you need.


    Another error you have is the way that you are reading in values. When you call

    cin >> s;
    

    It won’t read in a full line, only a single token. Also, you’re only ever setting the numerator of your fractions. Thus, you have two options:

    1. Use getline, to read in a full line and then parse it from there.

    2. Just use cin >> more effectively, and do something like:

    –

    while (cin >> s)
    {
        char* frac = s;
    
        if (!strcmp(s, "0")) break;
    
        char* num = strtok( frac, "/" );
        char* denom = strtok( NULL, "/");
    
        fractions[count].num = atoi(num);
        fractions[count].den = atoi(denom);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment for school: I need to make a user database. First
I am working on an mySQL assignment for school and I am stuck on
For a school assignment me and a friend have been working on rendering a
I was working on a school assignment (doing Hoffman encoding) and I need some
I have this school assignment that I've been working on and am totally stumped
I am working on a school assignment in java and i have come across
I am working on a school assignment that requires me to be able to
I'm working on a school assignment and I've been completely stumped with this step.
In school I am part of a team of four working to create a
I'm working with a Ruby project for school, and have sadly not been able

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.