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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:17:42+00:00 2026-06-12T11:17:42+00:00

I’m having some issues using the atoi(s) function. I’m trying to convert a command

  • 0

I’m having some issues using the atoi(s) function. I’m trying to convert a command line argument into an integer, but the integers I am receiving from the atoi function are acting strange. I have checked the variables holding the conversion and they hold the correct integers, but when they run through my program they do not work as they should. The program accepts three arguments; the program itself, the function number(1-3), and an arbitrary integer. For example, the command line instruction would look like lab4p2 3 10.

Functions:

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


//---------------------------------------------------------------------------
// Functions

// Sumation function adds all integers from 1 to the value.
int sumation(int x)
{
  int counter = 1;
  int value;
  while (counter < (x+1))
    {
      value += counter;
      counter++;
    }
  return value;
}

// Negation function returns the negation of the given value.
int negation(int x)
{
  int negator, value;

  negator = (x*2);
  value = (x - negator);
  return value;
}

// Square function returns the square of the input value.
int square(int x)
{
  int value;
  value = (x*x);
  return value;
}

//---------------------------------------------------------------------------

Main:

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

  int inputval, functionval, value;
  double doubleval;

  functionval = atoi(argv[1]);

  inputval = atoi(argv[2]);
  doubleval = atof(argv[2]);

  printf("%i", functionval);
  printf("%i", inputval);

  if(argc != 3)
    {
      printf("%s", "Invalid amount of arguments! 3 expected. \n");
    }
  else if ((functionval < 1)||(functionval > 3))
    {
      printf("%s", "Invalid function value. Expected values: 1-3 \n");
    }
  else if (inputval != doubleval)
    {
      printf("%s", "Invalid value! Integer expected. \n");
    }
  else if (functionval = 1)
    {
      value = sumation(inputval);

      printf("%s", "The sum from 1 to the input value = ");
      printf("%d", value);
    }
  else if (functionval = 2)
    {
      value = negation(inputval);

      printf("%s", "%d", "The negation of the input value = ", value);
    }
  else if (functionval = 3)
    {
      value = square(inputval);

      printf("%s", "%d", "The square of the input value = ", value);
    }
  else
    {
      printf("%s", "Something is wrong!");
    }

}

All of the error checks work correctly, but functions 2 and 3 are never accessed (despite input) and function 1 displays an incorrect answer. Does anyone know what the issue may be?

Thanks,
Matt

  • 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-12T11:17:44+00:00Added an answer on June 12, 2026 at 11:17 am
    functionval = 1    assignment
    
    functionval == 1   comparison
    

    By the way, there’s a typo when you get doubleval, too
    Now that I see that using argv[2] again with a different conversion function was deliberate, rather than a typo for argv[3], I’ll instead suggest that if you care about conversion errors, you use something like:

    errno = 0;
    char* endptr;
    long value = strtol(argv[2], &endptr, 10);
    if (errno == ERANGE) {
      // Value was too big or too negative
    } else if (endptr == argv[2] || errno == EINVAL) {
      // Not a number
    } else if (endptr[0] != '\0') {
      // trailing characters.
    }
    // Value is OK.
    

    which, if you use it a lot, is worth wrapping into something easier to call.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.