C++ :: Range Based For Loops?
Aug 7, 2014explain Range based for loops ?
View 7 Repliesexplain Range based for loops ?
View 7 RepliesIn Particular:
N3337 wrote:86) this ensures that a top-level comma operator cannot be reinterpreted as a delimiter between init-declarators in the declaration of __range.
What in the world would be a valid example of when this might occur? (IE one that isn't blatantly misusing the quirks of the language).
This topic can also serve as a review topic on this presentation as well: [URL] .....
This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.
This is my working program:
#include <string>
#include <iostream>
int main(int argc, char *argv[]) {
if (argc > 1) {
std::string concatenatedArgs;
[Code] ....
Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.
#include <string>
#include <iostream>
int main(int argc, char *argv[]) {
if (argc > 1) {
std::string concatenatedArgs;
[Code] ....
I would like to try out a range based for loop. I am using gcc 4.6.3. According to the link below, gcc 4.6.3 should allow me to use a range based for loop.
[URL]
However when attempting to run the code below, my IDE (Eclipse) reports the following error:
"error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options:
int a[5] ={1,2,3,4,5};
for (int x : a) {
cout<<x;
}
If gcc 4.6.3 supports range based for loops why do I get this error?
In the traditional for loop, you could make the loop start again by resetting the int value
for (int i = 0; i < 10: ++i){
//Do something
i =0;
}
Then it would start the loop again. However I can not seem to find a way to do this with the range based for loop. Is there anyway to force a range based for loop to start from, i = 0 ?
for(const auto &i : vec){
//Do something
//Restart for loop
}
Is it possible to create a class that stores (non-const) references to some objects and enables users direct access by using range-based for loops on them?
Code: class container {
public:
void add(int& value);
void remove(int& value);
...
};
int main()
{
container c;
for (auto& value:c) {
// `value' should be accessible as type `int&' instead of being a pointer, `std::reference_wrapper<int>' or something like that
}
}
I'm using an animation program. In this program I've simulated a particle system. The particles are flying around at different and varying speeds. I've attached birds to the particles and I want to be able to control each bird's flapping animation based on its velocity; so birds moving faster will be flapping faster.
Initially, the bird's flapping animation is controlled by a parameter that goes from 0 to 100%. So not only do I need to drive the speed at which the animation goes from 0 to 100%, I need to set it on a loop so once it reaches 100%, it loops back to 0%. I'm extremely new to code so I don't think it would be wise for me to even provide a jumping off point, not that I could.
I this notation:
for (; *strings[i]; i++)
the same as:
do {
i++
} while(*strings[i]);
?
I have an assignment that calls for a C program using a for loop AND a while loop that will receive an integer (called daNumba) and double it -- Using the integer the program will call the sumFor function and then the sumWhile function. These functions will both sum the values from daNumba to (daNumba * 2) ifdaNumba is positive. If daNumba is not positive it will add the values from (daNumba*2) to daNumba. Both functions will receive the value of daNumba and return a summed value. The only difference between the 2 functions is that sumFor will only use for loops and sumWhile will only use while loops. We are not to use arrays.
The program compiles without error. So far my while loop works for positive integers, but not with a negative integer (I have it commented out) I cannot get the for loop to work properly This is what I have so far -- I am stuck....
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {
[Code] ....
See code below:
#include <stdlib.h>
#include <stdio.h>
int main ( int argc, char *argv[] ) {
int P[150] = {}, i, j;
for ( i = 2; i <= 150; i++ ) {
[Code] .....
Using gdb, I noticed that the variable j keep going back to initial value after the interior for loop condition returns false. Why doesn't this for loop terminate right away?
I've just started learning the C language, and I'm stuck on something that is probably quite simple.how to get IF statements working within WHILE loops. My code is below. I am trying to write a program to count the number of words in a sentence, and obviously stop counting after a full stop has been entered. I created a variable called 'spaces' which will increase by one after the user enters a space. However, when the IF statement is in place, the loop never terminates, even if I enter a full stop. When I delete the IF statement, the loop functions correctly.
Code:
#include <stdio.h>
int main()
{
char x;
char a;
char y;
int spaces = 0;
}
[code]....
What output would you expect from this program?" The output was not what I expected. I've psuedo-coded this out and I'm still missing something.
Code:
#include <stdio.h>
int main () {
int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int i, j;
}
[code]....
The output: Code: 1 1 2 4 8 16 32 64 128 256 So when I look at this first loop I see that j = 0, which is less than 10, so then the program statement should commence, which is another for loop. So in this inner for loop I see that i = 0, which is not less than j, so this loop should terminate. Then the value of j increments by 1 and the first go around of the loop has completed.
Now I see that j = 1, so this is less than 10, and the inner for loop commences once again. This time though, i actually is less than j, so numbers[1] = numbers[1] + numbers [0], or numbers[1] = 0 + 1. Now the value of i is incremented by 1 and the first go around of this inner loop has completed. Then the value of j increments by 1 and another go around of that loop has completed.
So now j = 2, i = 1, and numbers[2] ( which is 0 ) = numbers[2] + numbers[1], or numbers[2] = 0 + 1. I was expecting the output to be an array full of 1's. However this is not the case..
I need to make a for loop without: using the math functions like pow, sqrt, etc. or an if-statement.I can only use the basic arithmetic functions like +,-,*, and.The for loop needs to display: 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192 as the result
How do I make it so my loop works? This is what I have and it doesn't work...and I have no clue how to continue...
k = 0; //k is an int and I have declared it in the beginning of my code
for( k = 0; k < 13; k++ ) {
printf ( "%d, ",k );
k = k * 2;
}
printf("%d", x);
I just want to say that I just started learning the language and this is my very first shot at writing a simple program that is not "Hello World" So I recently learnt the basics of the if statement and how to loop in a console application, here's what it looks like:
namespace Testing_IF_Statement
{
class Program
{
[Code].....
And again, this is just what I wrote in a few minutes without putting any thought into it.
Questions:
1)I have heard that the way I'm looping by using the goto statement is considered quite ancient, why is this and how else could I loop the program?
2)I declared a as an integer and asked the user to type 1,2,3,4 to perform mathematical functions, but when I tried declaring a as a string and searching if a == "PLUS" etc it would throw an error. Is there any mistake in how I approached this?
3)I know this is a bit premature to ask, but instead of writing _________ in the console to separate the loop is there any professional way to add a separator?
4)In general how can I improve the code?
im trying to make a program which will tell which key i pressed and then it will print in msgbox, but i cant figure out how to read the key in loops, like i tried getasynkey (imported to my c# console app) with value around -32676 or -32767 i ( i found it on internet) and it only shows the key once, i want to do like while my key is pressed, then it will spam my console with key pressed, is there any way to do it?
View 6 Replies View RelatedMy program behaves weird... I wanted to generate 10 random numbers from 1 to 100 each of them bigger than previous, using the while loop and function that returns a random number in specified range.
When I run the program, I get numbers much bigger than 100, even negative number, and numbers are same every time I run the program.
Code:
#include <ctime>#include <cstdlib>
#include <iostream>
using namespace std;
int range(int low, int high);
[Code] .....
What the range of values and how to calculate them?
int Num = rand() % 350 + 13 / 10
i have to find 2 random values between a range, lets say from 0-3 i have to find all the possible combinations between this range like (0,0),(0,1)...etc But, it has to be RANDOM and the same combination cannot repeat it self(obviously).
View 8 Replies View RelatedI have an assignment where I have to use two for loops. I need to ask the user for any two numbers and be able to list all the numbers in between and their factors and state whether or not the number is prime or not.
View 2 Replies View RelatedI've been debugging this program since yesterday and I continue to run into a string subscript error. I pasted the code in a pastebin (it's only 400 lines), to see why I'm getting this. The problem seems to come up during a debug assertion failure.
[URL] ....
whenever I try to use either <string> or any STL container. Everyone I saw so far, says that "using a .reserve(n)" before adding items to random positions is enough. However, each time I run the code, I still get the same error, unless I actually write the memory with some initial data, and only after access random positions.I am fully aware of the fact that the STL containers and <string> are dynamic data types and memory allocation is done dynamically. However, if I need to allocate all those memory slots before knowing how many I need, would lead me to the same memory complexity as using a char [] array (which is static -- size declaration at first).
how is it possible to keep the <string> dynamic, while being able to add elements on random positions (if possible). I know the strings have the ending char '', but there should still be something that would allow it to work. Okay, long story short, here is my example. I am trying to read from file rows of data. The first char on each row represents a normal char c. The rest of the row is a string which contains numbers (integers between 1 and 250) which represent the position at which the char c (read before) will have its location.
For example the input file:
#include <fstream>
#include <deque> // for later use
#include <string>
#include <sstream>
#include <algorithm> // for later use
[code].....
The program works perfectly, if instead of text.reserve(250); I use text.resize(250);. However, what is the difference between the two? I mean, why isn't reserve enough?
I keep getting this "Debug Assertion Failed" error that says:
expression: vector subscript out of range
I tried to make the loop the same as the vector size, but I'm still getting the same errors.
void Grid::output(ostream & out) {
vector<vector<int>> grid(4);
int rows, columns;
out << " 0 1 2 3 " << endl;
out << " +---------+" << endl;
for( rows=0; rows<grid.size(); ++rows ) // make each row
[code]....
In my program I have a range check setup in the class I call from main, but when I run it and put in a value > or < than the min/max it just calculates anyway. Where have I made a mistake?
Heres the Main
#include "box_class.h"
#include <iostream>
using namespace std;
int main() {
double length;
double width;
double height;
double volume;
[Code] ....
I'm making a simple game and I'm having trouble creating boundaries for the players movements. The map is a 2D vector. When I compare the vector with the players current position sometimes I get an error during run. When the error isn't occurring the behavior of the boundaries is bizarre. The error tells me that the vector is out of range.
Here is the where the map is created. It's within its own class constructor.
vector< vector<char> > map_parts;
map_parts.resize(25);
for ( int i = 0; i < 25; i++ )
{
[Code].....
Ok so I'm reading the Programming: Principles and Practice using C++ and Im stuck in Drill 4 part 5. It says:
Change the program so that it writes out the "numbers are almost equal" after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/10000000
I'm using an If statement for it... I just need know what the formula is to check 2 numbers that were entered by person if they land within the range specified above. so then I can cout << "numbers are almost equal" << endl;
I'm trying to do some operator overloading, the function is supposed to add the values at index [i] of two vectors and place the result in the returning vector. The problem is I keep getting a vector out of range. This is the overloaded operator I'm working with (relatively new to these):
vector<float> operator+(const vector<float>& a, const vector<float>& b){
unsigned long size;
vector<float> temp;
if(a.size() >= b.size())
size = a.size();
[Code] .....
and then I would do something like this in the main:
vector<float> v, v1, v2;
v1.push_back(9.1);
...
v2.push_back(8);
...
v = v1 + v2;
but when I try to output the vector v I just get a vector out of range exception.