Array/Loop assistance?

1. Declares an array of 5 integers and initializes each position to 0

2. Use a loop to prompt the user to enter the data into each position of the array.

3. User entry should be a string then converted to proper data type.

So this int ara [5] = { }; is an array that initializes each position to 0, right?

What type of loop is best for entering the data and how do I even go about setting it up for this? While loop?

Pretty sure I can convert the data that I need using atof or something but I don't really know how I'd set something like this up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
 //--------  Start of variable and object creation ------
 string array1 = "XXXX";
 string array2 = "XXXX";
 string array3 = "XXXX";
 string array4 = "XXXX";
 string array5 = "XXXX";

 int ara[5] = { };
 }




Edit: Okay I got your guys responses and will try and figure more out for now, ty.
Last edited on
is an array that initializes each position to 0, right?
Correct.

What type of loop is best for entering the data and how do I even go about setting it up for this?
Typically, if you are iterating a fixed number of times, this is best communicated with a for loop.
http://www.cplusplus.com/doc/tutorial/control/
1
2
3
4
5
6
for (int i = 0; i < 5; i++)
{
    // do stuff with ara[i]
    // e.g.
     ara[i] = 42 + i;
}


convert the data that I need using atof
Since you're dealing with integers, you can use atoi instead. (Or, stoi in C++11, which can handle '0' values)

So... first, get input:
1
2
string input;
cin >> input;

Then convert that input to an int:
int n = atoi(input.c_str());
(The "c_str()" converts the string to a format that is recognizable by C library functions like atoi)

Then use that n and store it in the appropriate spot.

Last edited on
std::atof() converts a C string to a double. You have an array of integers.

You are supposed to use a std::string as your input (instruction 3), use <string>'s std::stoi to convert a string to an int.
http://www.cplusplus.com/reference/string/stoi/

Do you know what type of loop you'd use to display the contents of an array? Use the same type of loop to get the input for each element.

HINT: use a for loop
https://www.learncpp.com/cpp-tutorial/63-arrays-and-loops/
Last edited on
Okay how about something like this:
1
2
3
4
5
6
7
8
int arr[5] = {};
for (int i = 0; i < 5; i++)
{
    cout << "#?" << i << ":";
    string input;
    cin >> input;
    arr[i] = stoi(input);
}
Hello Murasame,

That will work to a point as long as the user always inputs a number. Should the string start with a letter it will fail with a run time error.

This may be ahead of what you have learned, but give it a run and see what happens:
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

int main()
{
	int arr[5]{};

	for (int i = 0; i < 5; i++)
	{
		std::cout << " Enter #" << i + 1 << ": ";

		std::string input;

		std::cin >> input;

		try
		{
			arr[i] = stoi(input);
		}
		catch (const std::exception& exc)
		{
			std::cout << "\n    " << exc.what() << "\n\n";

			i--;
		}
	}


	// <--- Keeps console window open when running in debug mode on Visual Studio.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

Enter "a123" and see what happens.

I rearranged your prompt a little. It makes more sense to a user.

Andy
Topic archived. No new replies allowed.