Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have written a script so that the odd numbers are stored in a vector and then displayed at the end after putting in a negative number.
For some reason the first odd number is only displayed. Here is the code:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int i=0;
int val=0;
int rem;
vector<int>v;
while(val>=0){
cout<<"Please enter an integer: \n";
cin>>val;
rem=val%2;
if(rem==1)
v.push_back(val);
else;
}
for(int i=0; i<v.size(); ++i);
cout<<"Odd Numbers: " <<v[i]<< "\n";
system("pause");
return 0;
}
Iterating through a vector is best done with an iterator.
for(auto it = std::begin(v); it != std::end(v); it++)
std::cout << "Odd number: " << *it << std::endl;
Be careful with your semicolons:
for(int i=0; i<v.size(); ++i);
// ^^^
cout<<"Odd Numbers: " <<v[i]<< "\n";
You are running the loop:
for(int i=0; i<v.size(); ++i) {
;
}
cout<<"Odd Numbers: " <<v[i]<< "\n";
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
Im fairly new to C++ and im trying to convert an int "city.temp[4]" to a string and then adding that to an already existing string "city.name[4]" which would then sort the cities and their temperatures based on low to high. The user is the one to name all four cities and assign them a temperature. The bubbleSort is working fine sorting the temperatures by them self but im not sure how to get the cities to follow along with their respective temperatures
#include <iostream>
using namespace std;
class City{
public:
string name[4];
int temp[4];
};
int main() {
City city;
city.name[4];
city.temp[4];
cout << " Please Input the name of 4 different cities and their temperature\n\n\n";
for(int i=0; i < 4; i++){
cout << " Input the name and temperature of city (" << i+1 << "): "; getline(cin, city.name[i]); cout << " "; cin >> city.temp[i];
cin.ignore();
}
int length = 4;
for(int i = 0; i < length; i++){
for(int j = 0; j < length - 1; j++){
if(city.temp[j] > city.temp[j+1]){
int hold = city.temp[j];
city.temp[j] = city.temp[j+1];
city.temp[j+1] = hold;
}
}
}
for(int i = 0; i < length; i++)
cout << " " << city.temp[i] << " " << city.name[i] << "\n";
return 0;
}
The good solution is to have a City-class with a name and a temperature and swap cities based on the order of the temperature with std::sort.
The easy fix for now is to use std::swap to swap the temperatures and at the same time swap the names:
if(city.temp[j] > city.temp[j+1]){
std::swap(city.temp[j], city.temp[j+1]);
std::swap(city.name[j], city.name[j+1]);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to reverse a vector using functions but whenever I run the program it terminates and outputs nothing. Can anyone please tell me what I am doing wrong?
#include <iostream>
#include <vector>
using namespace std;
void reverseavector(vector<int>& vec)
{
for (int i = vec.size() - 1; i >= 0; --i) {
cout << vec[i];
}
}
int main()
{
int input;
vector<int> vect;
cout << "Enter values to reverse the vector" << endl;
cin >> input;
for (int i = 0; i < vect.size(); ++i) {
vect.push_back(input);
}
reverseavector(vect);
return 0;
}
In your code, you declared the following statement:
vector<int> vect;
This statement means that a vector of integers named vect get created, but you didn't specify a size for it, therefore when the following loop is reached:
for (int i = 0; i < vect.size(); ++i){
vect.push_back(input);
}
vect.size() return 0 because you didn't specify a size for vect, therefore the loop never gets executed. Because the condition is false.
I made some changes to the code you have. Take a look:
#include <iostream>
#include <vector>
using namespace std;
void reverseavector(vector<int>& vec)
{
for (int i = vec.size() - 1; i >= 0; --i) {
cout << vec[i];
}
}
int main()
{
int input;
vector<int> vect;
bool stop = false; // This bool will keep allow the while loop to keep prompting the user to enter a number until the input is -1
while (stop == false)
{
cout << "Enter a value to the vector to be reversed, or -1 to stop entering values" << endl;
cin >> input;
vect.push_back(input);
if (input == -1)
{
stop = true;
}
}
reverseavector(vect);
return 0;
}
The main thing about your code is that you needed a way to loop taking values into input. There are other ways to do this, but this was the first one I came up with.
When you run
vector<int> vect;
cout << "Enter values to reverse the vector" << endl;
cin >> input;
for (int i = 0; i < vect.size(); ++i)
vect.size() remains at 0, because the vector is empty. So, i < vect.size() remains false, and the loop doesn't enter.
So, reverseavector gets an empty vector and outputs nothing.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
For some reason, myprogramminglab keeps saying that I have not declared what 'i' and I don't understand why.
#include <iostream>
using namespace std;
int main()
{
int years;
double cost, inflaRate;
cout<<"Enter the current price of pencils:";
cin >> cost;
cout<<"Enter the number of years in the future that
you will buy the pencil:";
cin >> years;
cout<<"Enter the inflation rate as a percentage." <<
endl;
cin >> inflaRate;
inflaRate /= 1;
cout << "The price of pencils will be " << cost;
for (i = 0; i <= years; i++) //Keeps telling me I have not declared 'i' here
{
cost += (cost*inflaRate);
}
cout << cost << "in" << years << "years." << endl;
system("pause");
return 0;
}
For some reason ...
That reason is because you haven't actually declared i. You can fix that with a simple change to your for loop:
for (int i = 0; i <= years; i++) // No longer complains you have not declared 'i' :-)
// ^^^
// Declare it!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
this my code but i get infinite loop with the first number
I want to read the integers fromt he file and store them in the array
the file contains:
8 5 12 1 2 7
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int n = 0; //n is the number of the integers in the file ==> 12
int num;
int arr[100];
ifstream File;
File.open("integers.txt");
while(!File.eof())
{
File >> arr[n];
n++;
}
File.close();
for(int i=0;i<12;n++)
{
cout << arr[i] << " ";
}
cout << "done\n";
return 0;
}
Any help please
I agree with #ravi but I some notes for you:
If you don't know how many integers are in the file and the file contains only integers, you can do this:
std::vector<int>numbers;
int number;
while(InFile >> number)
numbers.push_back(number);
You need to #include<vector> for this.
it would be better if you read how many integers are in the file and then use loop to read them:
int count;
InFile >> count;
int numbers[count]; //allowed since C++11
for(int a = 0; a < count; a++)
InFile >> numbers[a];
Note: I didn't check for successful read, but it is a good practice to do so.
Your loop should be:-
for(int i=0; i < n ; i++)
{
cout << arr[i] << " ";
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
this my code but i get infinite loop with the first number
I want to read the integers fromt he file and store them in the array
the file contains:
8 5 12 1 2 7
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int n = 0; //n is the number of the integers in the file ==> 12
int num;
int arr[100];
ifstream File;
File.open("integers.txt");
while(!File.eof())
{
File >> arr[n];
n++;
}
File.close();
for(int i=0;i<12;n++)
{
cout << arr[i] << " ";
}
cout << "done\n";
return 0;
}
Any help please
I agree with #ravi but I some notes for you:
If you don't know how many integers are in the file and the file contains only integers, you can do this:
std::vector<int>numbers;
int number;
while(InFile >> number)
numbers.push_back(number);
You need to #include<vector> for this.
it would be better if you read how many integers are in the file and then use loop to read them:
int count;
InFile >> count;
int numbers[count]; //allowed since C++11
for(int a = 0; a < count; a++)
InFile >> numbers[a];
Note: I didn't check for successful read, but it is a good practice to do so.
Your loop should be:-
for(int i=0; i < n ; i++)
{
cout << arr[i] << " ";
}