answersLogoWhite

0


Best Answer

#include<iostream>

int main()

{

int n[4];

std::cout << "Enter 4 integers: "

std::cin >> n[0] >> n[1] >> n[2] >> n[3];

}

The above is naive because there's no error-checking to prevent garbage input. A better solution is to input 4 strings and then convert those strings to integers. If the conversion fails, simply ask for new input until the conversion succeeds. The following shows one method of achieving this:

#include<iostream>

#include<sstream>

int main()

{

int n[4];

// repeat until valid

bool valid {false};

while (!valid)

{

// create 4 temporary strings for input

std::string s[4];

std::cout << "Enter 4 integers: "

std::cin >> s[0] >> s[1] >> s[2] >> s[3];

// assume input is valid until proven otherwise

valid = true;

for (size_t i=0; valid && i<4; ++i)

{

// convert the current string to an integer

std::stringstream ss {s[i]};

valid = (ss >> n[i]);

}

}

// When we reach here, n[] will contain 4 valid integers.

// Use n[] ...

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to wite the codes in C plus plus enter 4 integers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

When was Wite-Out created?

Wite-Out was created in 1966.


Where does the senator work at?

in the wite house in the wite house


Is a wite persons dich wite?

yes sure


Can you smoke China wite?

No no


is wite a adverb?

No, white is not an adverb, because white is an adjective.


What is a elbino?

this is a tan wite person


How do you wite one trillion?

1000,000,000,000


Was MJ wite or black?

he was white


How do you wite 165 billion?

165,000,000,000


What does Santa Claus look like in Ukraine?

samta looks like a person with a wite brid and wite hair and that is what santa looks like


How can you wite forty seven thousdon?

47,000


Is the presidents offes in the wite house?

Yes it is