STRING CONVERSION TO INTEGER

Write your question here.
How can I change a string to an integer using atoi?
See this line: int n_Dolphins = atoi(agrv[1]);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 /*	dolphins
*	Write a program that takes one and only one comand line argument: the number of dolphins to be trained (>0). 
*	Allow the trainer to enter an age for each dolphin (>0).  To get an age call a function with
*	the following prototype:
*					int* getAge(void)
*	All ages should be stored in array in main.
*	Finally, print out the age of the oldest dolphin.
*					pseudocode
*	Use int main(int argc, string argv[])
*	command-line argument will be the number of dolphins
*	test is number > 0
*	if <= 0, exit
*	if > 0, prompt traner to enter age of each dolphin
*	use the function int* getAge(void)
*	store the ages in an array in main: use int age[10];
*	Find the oldest dolphin and print the result
*/

#include <cstdio>
#include <string>
#include <cstdlib>
using namespace std;

// function prototype
int* getAge(void);

int	main(int argc, string argv[])
{
	// array to store ages
	int age[50];

	// change agrv[1] to a number using atoi
	int n_Dolphins = atoi(agrv[1]);

	// check if number > 0
	if (n_Dolphins > 0)
	{
		for (int i = 0; i < n_Dolphins; i++)
		{
			age[i] = *getAge();
		}
	}
	else
	{
		exit(EXIT_FAILURE);
	}

	// find the oldest
	int oldest = 0;
	for (int i = 0; i < n_Dolphins; i++)
	{
		if (age[i] > oldest)
		{
			oldest = age[i];
		}
	}
	printf("The oldest dolphin's age is: %i\n", oldest);
}
1
2
// int	main(int argc, string argv[])
int main( int argc, char* argv[] )

http://en.cppreference.com/w/cpp/language/main_function
JLBorges,

Thank you for coming to my rescue for the second time. I made this same mistake before. I had to brush up my program and it works. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//	dolphins
/*	Write a program that takes one and only one comand line argument: the number of dolphins to be trained (>0). 
*	Allow the trainer to enter an age for each dolphin (>0).  To get an age call a function with
*	the following prototype:
*					int* getAge(void)
*	All ages should be stored in array in main.
*	Finally, print out the age of the oldest dolphin.
*					pseudocode
*	Use int main(int argc, string argv[])
*	command-line argument will be the number of dolphins
*	test is number > 0
*	if <= 0, exit
*	if > 0, prompt traner to enter age of each dolphin
*	use the function int* getAge(void)
*	store the ages in an array in main: use int age[10];
*	Find the oldest dolphin and print the result
*/

#include <cstdio>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

// function prototype
int* getAge(void);

// Gobal variable
int xx = 1;


int	main(int argc, char* argv[])
{
	// array to store ages
	int age[50];


	// change agrv[1] to a number using atoi
	int n_Dolphins = atoi(argv[1]);
	
	// check if number > 0
	if (n_Dolphins > 0)
	{
		for (int i = 0; i < n_Dolphins; i++)
		{
			age[i] = *getAge();
		}
	}
	else
	{
		exit(EXIT_FAILURE);
	}

	// find the oldest
	int oldest = 0;
	for (int i = 0; i < n_Dolphins; i++)
	{
		if (age[i] > oldest)
		{
			oldest = age[i];
		}
	}
	printf("The oldest dolphin's age is: %i\n", oldest);
}


int* getAge(void)
{
	int x = 0;
	int* dolage = &x;
	printf_s("Please enter age of dolphin number %i: ", xx);
		cin >> x;
		xx++;
	return dolage;
}
Topic archived. No new replies allowed.