How do I save the input form the user into an array?

Here is my assignment:
create a program that reads in 5 numbers, find their sum and then print them in reverse. Use array as a tool in your program.

Here is what I have so far:

#include <iostream>
#include <stdlib.h>
using namespace std;

// This program reads in five numbers, find their sum, then prints the numbers in reverse.//


int main ()
{




    const int numlist = 2;
    int num[ numlist];
    int total ;
    int numcount;








    for (numcount = 0; numcount < 2; numcount ++)

{

    cout << " Enter five numbers: " << endl;
    cin >>  num[2];
    cout << num[2];
}

    for ( int i =0; i< numlist; i++)
        total = num[i];

    cout << "\nThe total of the numbers are: " <<total  << endl;

    return 0;
}

Dani AI

Generated

A concise diagnosis and fixes for the two problems seen in this thread (array input/sum/reverse and the grade program), tying together points from and .

The array program mistakes: an incorrect array index in the input loop (always writing to the same slot), the wrong numlist value, and an uninitialized total. Initialize accumulators (total = 0), use the loop index when reading (cin >> num[numcount]), and iterate from numlist - 1 down to 0 to print in reverse. Prefer std::vector<int> or std::array<int,N> for safety and use std::accumulate for sums and reverse iterators to print backwards, as suggested.

About the compiler error C2374 ("redefinition; multiple initialization"): this means the same identifier was declared twice in the same scope. Older MSVC versions put a for (int i = ...) variable into the surrounding scope, so a later for (int i = ...) triggers C2374. Two robust fixes: declare the loop variable once outside the loops (int i;) and reuse it, or use different variable names for each loop. On modern compilers the int inside each for is scoped to that loop and redeclaration is allowed; 's suggestion to reuse an earlier i works because it avoids a second declaration.

The grade program needs to actually pass data between functions and to use ranges, not exact matches. getScore() should return the entered score and printGrade() should accept it. Matching ranges is easiest with switch(score/10) or plain if ranges; also validate input. Example:

int getScore() {
    int score;
    std::cout << "Enter student's score (0-100): ";
    while (!(std::cin >> score) || score < 0 || score > 100) {
        std::cin.clear();
        std::cin.ignore(10000, '\n');
        std::cout << "Enter a number between 0 and 100: ";
    }
    return score;
}

void printGrade(int score) {
    switch (score / 10) {
        case 10: case 9: std::cout << "A\n"; break;
        case 8: std::cout << "B\n"; break;
        case 7: std::cout << "C\n"; break;
        case 6: std::cout << "D\n"; break;
        default: std::cout << "F\n"; break;
    }
}

Test with edge cases (0, 100, non-numeric input) and prefer a modern compiler or explicit outer-scope declarations to avoid scope-related compiler errors. This ties together the corrections from and the STL suggestion from .

Recommended Answers

All 7 Replies

#include <iostream>
// #include <stdlib.h> // You dont need this
using namespace std;

// This program reads in five numbers, find their sum, then prints the numbers in reverse.//
int main ()
{
	const int numlist = 5;
	int num[ numlist];
	int total = 0 ; // Remember to initialize variables
	int numcount; 
	for (numcount = 0; numcount < numlist; numcount++)
	{
		cout << "Enter number " << numcount << ": ";
		cin >> num[numcount];
	}
	for ( int i =0; i< numlist; i++)
	{
		total = total + num[i];
	}
	cout << "\nThe total of the numbers are: " <<total << endl;
	// To Print the numbers in reverse
	for ( int i =numlist - 1 ; i>=0; i--)
	{
		cout << num[ i ] << endl;
	}
	
	return 0;
}

Hello Wolfpack,

thank you for responding so promptly. I editted the code but I am still having some problems executing it. I keep getting an error message:

Array.cpp
C:\Documents and Settings\Administrator\My Documents\Array.cpp(26) : error C2374: 'i' : redefinition; multiple initialization
C:\Documents and Settings\Administrator\My Documents\Array.cpp(19) : see declaration of 'i'
Error executing cl.exe.

Array.obj - 1 error(s), 0 warning(s)

******************************************

Can you tell me what it means?

Akisha

#include <iostream>
// #include <stdlib.h> // You dont need this
using namespace std;

// This program reads in five numbers, find their sum, then prints the numbers in reverse.//
int main ()
{
	const int numlist = 5;
	int num[ numlist];
	int total = 0 ; // Remember to initialize variables
	int numcount; 
	for (numcount = 0; numcount < numlist; numcount++)
	{
		cout << "Enter number " << numcount << ": ";
		cin >> num[numcount];
	}
	for ( int i =0; i< numlist; i++)
	{
		total = total + num[i];
	}
	cout << "\nThe total of the numbers are: " <<total << endl;
	// To Print the numbers in reverse
	for ( int i =numlist - 1 ; i>=0; i--)
	{
		cout << num[ i ] << endl;
	}
	
	return 0;
}

Edit this

for ( int i =numlist - 1 ; i>=0; i--)
{
	cout << num[ i ] << endl;
}

to this

for ( i =numlist - 1 ; i>=0; i--)
{
	cout << num[ i ] << endl;
}

Disregard the last thread. I figured it out.

use STL instead, this is a job for vector.
Stroustrup himself advises against using arrays whereever STL can be used instead.

can you help with this program?

#include <iostream>

using namespace std;


// This program takes the numerical score and outputs a letter grade.//

int getScore ()
{


    int count;
    int score;

    for (count = 0; count <1; count ++){
    cout << "\nEnter the student's score: "<< endl;
    cin >> score;

    }


    cout <<"\nThe score is/are: " << score << endl;
    return 0 ;
}




int printGrade()

{
    int grade =0;
    int count;




    for (count = 0; count <1; count ++){ 


    switch(grade)
{
   case 90:
        cout << "\nYour grade is A.\n" << endl;
      break;
   case 80:
        cout << "\nYour grade is B.\n" << endl;
    break;
   case 70:
       cout << "\nYour grade is C.\n" << endl;
       break;
    case 60:
       cout << "\nYour grade is D.\n" << endl;
       break;
    case 50:
       cout << "\nYour grade is F.\n" << endl;

    }
    }


return 0;

}

int main ()
{

    getScore ();
    printGrade();













 return 0;
}

I'm stuck again. Your asistance is greatly appreciated.

#include <iostream>

using namespace std;


// This program takes the numerical score and outputs a letter grade.//

int getScore ()
{


    int count;
    int score;

    for (count = 0; count <1; count ++){
    cout << "\nEnter the student's score: "<< endl;
    cin >> score;

    }


    cout <<"\nThe score is/are: " << score << endl;
    return 0 ;
}




int printGrade()

{
    int grade =0;
    int count;




    for (count = 0; count <1; count ++){ 


    switch(grade)
{
   case 90:
        cout << "\nYour grade is A.\n" << endl;
      break;
   case 80:
        cout << "\nYour grade is B.\n" << endl;
    break;
   case 70:
       cout << "\nYour grade is C.\n" << endl;
       break;
    case 60:
       cout << "\nYour grade is D.\n" << endl;
       break;
    case 50:
       cout << "\nYour grade is F.\n" << endl;

    }
    }


return 0;

}

int main ()
{

    getScore ();
    printGrade();













 return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.