Solved

a newcomer to c programming needs help again

Posted on 2004-04-05
3
264 Views
Last Modified: 2010-04-17
i have once again come to a dead end with my logon program in c.
whatever value is given to my program it outputs the results for 2.
i have created a simpler program that produces the same error:

#include stdio

int main(int val1)
{
if(val1==1)
{
printf("1");
}
if(val1==2)
{
printf("2");
}
if(val1==3)
{
printf("3");
}
if(val1==4)
{
printf("4");
}
}

it is probably just me being stupid but can anyone explain this?
0
Comment
Question by:ico2
  • 2
3 Comments
 
LVL 1

Expert Comment

by:llewelm
Comment Utility
The main() function takes two parameters as follows:

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

The first parameter is the number of command line arguments, the second are the actual arguments.  My guess is that you've compiled your program and have a c.exe or something similar and your typing

c 1

and "2" is coming out.  That is correct because your program has two command line arguments -- "c" and "1".

Your program should look more like:

#include <stdio.h>

int main(int argc, char *argv[])
{
  if (strcmp(argv[1], "1") == 0) {
   printf("1");
  }
  if (strcmp(argv[1], "2") == 0) {
   printf("2");
  }

etc...
0
 
LVL 4

Accepted Solution

by:
PerryDK earned 20 total points
Comment Utility
Output of program ran with c.exe 1 2 3 -5 -10 20 15 8

ptrValues[0] = 1
ptrValues[1] = 2
ptrValues[2] = 3
ptrValues[3] = -5
ptrValues[4] = -10
ptrValues[5] = 20
ptrValues[6] = 15
ptrValues[7] = 8

The Sum of the numbers is 34

Actual program:

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

int main(int argc, char* argv[])
{
  if(argc < 2)
  {
    printf("improper usage.  Please pass some integer values to this program");

    //return -1 indicating failure
    return -1;
  }

  int numValues = argc -1;
  int* ptrValues = new int[numValues];
  for(int i = 0; i < numValues; i++)
  {
    //convert the paramters passed in to integers and store
    //them in our array for later usage
    //Note that if the string can not be converted to an integer
    //the value stored will be 0
    ptrValues[i] = atoi(argv[i+1]);
  }

  //lets enumerate through all the values displaying them to the user
  //and lets add them up just for fun
  int sum = 0;
  for(int i = 0; i < numValues; i++)
  {
    printf("ptrValues[%i] = %i \n", i, ptrValues[i]);
    sum += ptrValues[i];
  }

  printf("\nThe Sum of the numbers is %i", sum);

  delete[] ptrValues;

  return 0;
}


By the way are using strictly the C language or are you using C++.  If your using strictly the C language which I'm not sure why you wouldn't be using C++ but if you are write me back and I can change the code to work with strictly C.  Basically the allocation and deallocation of the ptrValues array will differ.  You'll have to use malloc and stuff like that which you don't have to use in C++.
0
 
LVL 4

Expert Comment

by:PerryDK
Comment Utility
Heres the program written strictly in C code if you need it.

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

int main(int argc, char* argv[])
{
  if(argc < 2)
  {
    printf("improper usage.  Please pass some integer values to this program");

    //return -1 indicating failure
    return -1;
  }

  int numValues = argc -1;
  int* ptrValues = (int*)malloc(numValues * sizeof(int));
  for(int i = 0; i < numValues; i++)
  {
    //convert the paramters passed in to integers and store
    //them in our array for later usage
    //Note that if the string can not be converted to an integer
    //the value stored will be 0
    ptrValues[i] = atoi(argv[i+1]);
  }

  //lets enumerate through all the values displaying them to the user
  //and lets add them up just for fun
  int sum = 0;
  for(int i = 0; i < numValues; i++)
  {
    printf("ptrValues[%i] = %i \n", i, ptrValues[i]);
    sum += ptrValues[i];
  }

  printf("\nThe Sum of the numbers is %i", sum);

  free(ptrValues);

  return 0;
}

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 40
word0 challenge 3 55
firstChar challenge 13 82
how to send memory stream from ics Client To ics server ? 11 46
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now