Make a program that receives 5 numbers and show the following output:. C++

Asked

Viewed 91 times

-1

I’m having difficulty formatting the output code is working right, someone has idea how to fix the output

#include <iostream>
#include <cstdlib>
#include <clocale>

using namespace std;

int main(){

    setlocale(LC_ALL, "");

    int seq[5], contS = 0;
    for(int i=1;i<=5;i++){
        cout << "Digite o " << i << "º número: ";
        cin >> seq[i];
        contS = seq[i] + contS;
    }

    cout << "Os números digitados foram: ";
    for(int i=1;i<=5;i++){
        cout << seq[i] << " + ";
    }
    cout << " = " << contS;


    return 0;
}

Input: 1 2 3 4 5

Expected exit: "The numbers typed were: 1+2+3+4+5 = 15"

3 answers

2


A shame to mix C with C++, but come on.

The last element cannot have the plus sign, so it has to be treated as an exception and not within the loop, because it has no reason to pay inefficiency with a branch within the loop.

Organizing the code a little better and correcting the error of access to indexes (which would not happen if using C vector++):

#include <iostream>
#include <clocale>

using namespace std;

int main() {
    setlocale(LC_ALL, "");
    int seq[5], contS = 0;
    for (int i = 0; i < 5; i++) {
        cout << "Digite o " << i << "º número: ";
        cin >> seq[i];
        contS += seq[i];
    }
    cout << "Os números digitados foram: ";
    for (int i = 0; i < 4; i++) cout << seq[i] << " + ";
    cout << seq[4];
    cout << " = " << contS;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2

I hope this satisfies you , I was breaking my head not to try to do the gambiarra

#include <iostream>
#include <cstdlib>
#include <clocale>

using namespace std;

int main(){

setlocale(LC_ALL, "");

int seq[5], contS = 0;
for(int i=0;i<5;i++){
    cout << "Digite o " << i+1 << "º número: ";
    cin >> seq[i];
    contS += contS;
}

cout << "Os números digitados foram: ";
for(int i=0;i<5;i++){
    if(i==4){cout<<seq[i];}
    else{cout << seq[i] << "+";}
}
cout << " = " << contS;
return 0;
}

0

EDIT:
The index range used was corrected.
In C++ arrays indexes start at 0.

Use a conditional ternary expression

//Se i < 4 insere uma string " + " na saída padrão, senão não faz nada.
cout << seq[i] << (i < 4 ? " + " : ""); 

In his example:

#include <iostream>
#include <cstdlib>
#include <clocale>

using namespace std;

int main(){

    setlocale(LC_ALL, "");

    int seq[5], contS = 0;
    for(int i=0;i<=4;i++){
        cout << "Digite o " << i + 1 << "º número: ";
        cin >> seq[i];
        contS = seq[i] + contS;
    }

    cout << "Os números digitados foram: ";
    for(int i=0; i<=4; i++){
        cout << seq[i] << (i<4 ? " + " : "");
    }
    cout << " = " << contS;


    return 0;
}

Exit:

Digite o 1º número: 1
Digite o 2º número: 2
Digite o 3º número: 3
Digite o 4º número: 4
Digite o 5º número: 5
Os números digitados foram: 1 + 2 + 3 + 4 + 5 = 15
  • 1

    Thanks buddy, I forgot about that operator

  • int i=1;i<=5 now that I’ve seen this is wrong. In c++ vectors span from Indice 0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.