i have to write a program which read from the keyboard a line of numbers and save them into an array, numbers have to be written just in a line, i wrote this but doesn`t work because of an infinite loop, any suggestion?
int main() {
int numCasos = 0, contNumCasos = 0, numElem = 0;
string aux;
cout << "Number of cases: " << endl;
cin >> numCasos;
while (contNumCasos < numCasos) {
cout << "Number of elements: " << endl;
cin >> numElem;
cout << "Enter the Elements separated by space: " << endl;
cin.ignore();
vector.cont = 0;
int i = 0;
while ((vector.cont < numElem) && getline(cin,aux,' ')){
vector.v[i] = stoi(aux);
vector.cont++;
i++;
}
}
cout << sumaBuenos(vector) << endl;
cin.ignore();
system("pause");
return 0;
}
An example:
console: Number of elements:
user: 4
console: Enter the Elements separated by space:
user: 2 43 65 56
--this has to be the vector
-- vector.v[0] = 2
-- vector.v[1] = 43
-- vector.v[2] = 65
-- vector.v[3] = 56
if you know how many numbers you have to read there is a simpler way:
int n;
cin>>n; // how many numbers;
vector<int> v(n);
for ( int i=0; i<n; ++i ){
cin>>v[i];
}
With a modification it works.
int main() {
int numCasos = 0, contNumCasos = 0, numElem = 0;
string aux;
cout << "Numero de casos: " << endl;
cin >> numCasos;
while (contNumCasos < numCasos) {
cout << "Numero de elementos: " << endl;
cin >> numElem;
cout << "Ingrese los elementos separados por espacios: " << endl;
cin.ignore();
vector.cont = 0;
getline(cin, aux);
istringstream iss(aux);
for (int i = 0;i < numElem;i++) {
iss >> aux;
vector.v[i] = stoi(aux);
vector.cont++;
}
cout << sumaBuenos(vector) << endl;
contNumCasos++;
}
system("pause");
return 0;
}
to someone who have the same problem.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "input numbers" << endl;
cin >> n;
vector<int> v(n);
for ( int i=0; i<n; ++i )
{
cin >>v[i];
}
return 0;
}
Related
I have to ask the user to put in an array size and then to ask the user to fill it out. When the user puts in a duplicate, the program should say "invalid" and the user is asked to replace the number. I am supposed to use traversing array search.
Like this example here:
Enter list size: 4
Enter value for index 0: 1
Enter value for index 1: 1
Invalid. Enter a new number: 2
Enter value for index 2: 5
Enter value for index 3: 6
This is my code so far:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter list size: ";
cin >> size;
int array1[size];
for (int i = 0; i < size; i++) {
cout << "Enter value for index " << i << ": ";
cin >> array1[i];
for (int j = i + 1; j < size; j++) {
if (array1[i] == array1[j]) {
cout << "Invalid! Enter a new value for index " << i << ": ";
cin >> array1[i];
}
}
}
return 0;
}
It does what was specified but the exercise probably was to write std::ranges::find.
#include <iostream>
#include <vector>
#include <cstddef>
#include <algorithm>
int main() {
size_t size;
std::cout << "Enter list size: ";
std::cin >> size;
std::vector<int> arr;
arr.reserve(size);
while(arr.size() < size) {
int t;
std::cout << "Enter value for index " << arr.size() + 1 << ": ";
std::cin >> t;
if (std::ranges::find(arr, t) == arr.end()) {
arr.push_back(t);
} else {
std::cout << "Invalid! ";
}
}
}
Try this approach, Every time user enter value helper function will check duplicate from already filled array
#include<iostream>
// Helper Function that will check duplicate from array
bool IsDuplicate (int arr[] ,const int idxSoFar, int element){
for(int i =0 ; i < idxSoFar ; i += 1 )
if( arr[i] == element){
std::cout << "Invalid! Enter a new value for index "<< idxSoFar + 1 << " : ";
return arr[i] == element;
}
return false;
}
int main () {
int size;
std::cout << "Enter list size: ";
std::cin >> size;
int array1[size];
for (int i = 0; i < size; i++) {
std::cout << "Enter value for index " << i << ": ";
do
std::cin >> array1[i];
while(IsDuplicate(array1 , i , array1[i]));
}
return 0;
}
I want to find the factors of a product by using arrays to check whether they equal it or not
the values do not print
here is the code
#include <iostream>
using namespace std;
int main() {
int arr[5] = { 1,3,5,7,2 };
int arr1[5] = { 0,6,5,4,9 };
int X;
cout << "Please enter X:"; cin >> X;
for (int i = 0, j = 0; i < 5 && j < 5; ++i, ++j) {
if (arr[i]*arr[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
Use this nested loops
for (int i = 0; i < 5 ;++i) {
for(int j=0 ;j<5;++j){
if (arr[i]*arr1[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
Most of the components for the array are in place.
I am however wondering what code is missing for the output to match what I am trying to do.
I tried searching for similar array coding. I would like to call the function and for the user to input numbers up to 20 different inputs.
#define size 20
using namespace std;
int i;
void Input(int student[]) {
for(int i = 0; i < size; i++)
cout << "Enter The Marks of Subject 2 of student no " << i + 1 << " ";
cin >> student[i];
}
void display(int student[]) {
for(int i = 0; i < size; i++)
cout << student[i];
}
int main() {
int student[size];
Input(student );
display(student);
return 0;
In your Input function:
void Input(int student[]) {
for(int i = 0; i < size; i++)
cout << "Enter The Marks of Subject 2 of student no " << i + 1 << " ";
cin >> student[i];
}
You not using brackets, so the cin >> student[i]; is outside of the loop. The i from the for loop is no longer in scope, so you are using the i here:
int i;
Which is never given a value, which leads to undefined behavior. Add brackets:
void Input(int student[]) {
for(int i = 0; i < size; i++) {
cout << "Enter The Marks of Subject 2 of student no " << i + 1 << " ";
cin >> student[i];
}
}
I am new to coding and trying to create a dynamic array that asks the user for the size of it and asks for input. It then should print. The part that I am having trouble with is that these procedures should repeat as needed or until they enter -1 for the number. I am having trouble with ending the program when they enter -1.
#include <iostream>
using namespace std;
int main()
{
int count;
cout << "How many values do you want to store in your array?";
cin >> count;
int *DynamicArray;
DynamicArray = new int[count];
for (int k = 0; k < count; k++)
{
cout << "Please input Values: ";
cin >> DynamicArray[k];
if (k == '-1') //This is the part i'm having trouble
{
cout << "The program has ended" << endl;
}
else
{
cout << endl;
}
}
for (int i = 0; i < count; i++)
{
cout << DynamicArray[i] << endl;
}
delete[] DynamicArray;
return 0;
system("pause");
}
//When I enter -1 as an input value, it continues to print it in the output. //I need it to end the program.
#include <iostream>
using namespace std;
int main()
{
int count;
cout << "How many values do you want to store in your array?";
cin >> count;
int *DynamicArray;
DynamicArray = new int[count];
for (int k = 0; k < count; k++)
{
cout << "Please input Values: ";
cin >> DynamicArray[k];
if (k == '-1') //This is the part i'm having trouble
{
if(DynamicArray[k]==-1){
delete[] DynamicArray;
cout << "The program has ended" << endl;
exit(0);
}
else
{
cout << endl;
}
}
for (int i = 0; i < count; i++)
{
cout << DynamicArray[i] << endl;
}
delete[] DynamicArray;
return 0;
system("pause");}
So I have a program that reads in a certain number of keys. An example would be
5 1 10 21 9 6 21 11 13 16 20
The text file example would be
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2
I want my program to start at Java and depending on first key, which in this case is 5, would move over 5 elements leading to "red". And so on and so on. However, I have managed to read in all the data and put them into arrays, I am just having trouble figuring out how to move the pointer in the array.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct pieces {
char word[5];
int jump;
} ;
// Main Function
int main ()
{
// declare variables
int wordCount[2];
int keyCount[2];
int numKeys, numKeys2;
int numWords;
int keyAmount = 1;
int wordAmount = 23;
int keyarr[11];
pieces cypher[wordAmount];
char filename[10];
ifstream inData;
int temp[10];
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
inData.open(filename);
if(inData.is_open());
{
// read in data
for ( numKeys = numWords = 0; numKeys < keyAmount; numKeys++){
// read in num of words and keys
inData >> wordCount[numKeys] >> keyCount[numKeys];
//read in words followed by jump key
for( numWords = 0; numWords < wordAmount; numWords++){
inData >> cypher[numWords].word >> cypher[numWords].jump;
}
// read in the actual keys
for( numKeys2 = 0; numKeys2 < wordCount[numKeys]; numKeys2++){
inData >> keyarr[numKeys2];
}
}
//print out data
for( int j = 0; j < numKeys; j++){
cout << wordCount[j] << "\n";
cout << keyCount[j] << "\n";
}
cout << "\n";
for ( int i = 0; i < wordAmount; ++i){
cout << cypher[i].word << " ";
cout << cypher[i].jump << " ";
}
cout << "\nKeys: " << "\n";
for(int k = 0; k < 11; k++){
cout << keyarr[k] << " ";
}
cout << "\n";
}
inData.close();
return 0;
}