C :: Variable Is Unexpectedly Set Zero While Running While Loop
Sep 4, 2013
I ran into a problem while using while loop.T he declared and initiated local int variable works well with its specified value while running through the 1st run of a while loop. It is set zero while entering the 2nd run and the following unexpectedly. However, the variable still exits. The following is the code with problem.
Code:
#include <stdio.h>
int main(void){
const int x=6;
char c='y';
[Code]....
View 10 Replies
ADVERTISEMENT
Feb 18, 2014
i am writing a function that takes a delimited string and splits the strings into it's respective words according to the delimeter.
1) iterate through string and store delimeter position in vector array.
2) iterate through again and use substr to split into words and store again in a vector.
I then have a dictionary file, and am comapring each values in the string with each in the dictionary, problem is that it overruns the loop and obviously gives a subscript out of range error.
Code:
#include <iostream>#include <fstream>
#include <vector>
using namespace std;
//Start with string inputString
//Find delimeters ::pos as ints and stores positions in vector <int> array
//Run though string using 'find' and seperate string by positions of char32s
//Build vector<string> array of individual words in string
//Open dictionary file and loop though testing each words in vector string array against each word in dictionary
[code]....
View 4 Replies
View Related
Aug 7, 2014
My program is supposed to end when both a and b are 1. Where it is wrong.
Code:
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int roll()
[Code] ....
View 2 Replies
View Related
Apr 14, 2013
This program is meant as practice. However, it is not working as intended. The for loop is meant to take the argument from the user and use it as the amount of times to loop. Instead it simply outputting: . It is supposed to output i++ but does not.
Here is the code:
main.cpp:
#include <iostream>
#include <stdlib.h>
int main(int main, char* arg[], char* arg2[]) {
int iterator = atoi(arg2[0]);
std::string z = "-c";
[Code] ....
View 19 Replies
View Related
Feb 28, 2013
I am stuck on an exercise where i am supposed to use a loop to take user input and keep a running sum until the user enters a 0. the code i have so far is:
#include <iostream>
int main() {
using namespace std;
int num;
int total = 0;
int x;
[Code] ....
The full text of the error message is: error c2678:binary'>>':no operator found which takes a left-hand operand of type 'std::istream' . and one more thing i was wondering, is there a difference between c++ and visual c++?
View 4 Replies
View Related
Sep 8, 2013
I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below.
"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.
char our_string[15] = "Hi there!";
(Please note this is a 'C-string', not C++ standard string.)"
View 5 Replies
View Related
Jun 14, 2014
I am trying to create a tennis tournament simulator. There are 128 players in the tournament, and 32 seeds. I have a vector filled with 'Player' objects named 'Players'. Each 'Player' object has three member variables, Strength, Ranking, and ATPPoints. When the vector 'Players' is first declared, the default constructor for each 'Player' object is used, which initialises each of the 'Player' objects to zero. I then generate a Strength value in the range of 0-10 for each of these 'Player' objects, and sort them descendingly according to their strength.
I now want to create the draw for a tournament. I declare a vector 'Draw' with 128 'slots'. Each slot initially contains a blank 'Player' object, with its member variables set to 0. I first place the top 32 players throughout the draw such that they do not meet until later rounds as they would be in real life. For the remaining players, I position them in the draw by generating a random number between 0 and 127. Some of the slots 0-127 are occupied by seeded Players. These seeded Players have non zero Strength and Rank values, so to avoid overwriting these slots, I check the Strength value of the 'Player' object in the slot in question. A return value of 0 would indicate an empty slot.
At the beginning of the function, I output the Strength and Rank values of the 'Player' objects already in the draw. This outputs correctly.
for (Player& p : Draw)
{// outputs the draw in it's current state, with the already placed seeds as expected.
cout << p.GetRanking() << " " << p.GetStrength() << endl;
}
[Code]....
View 7 Replies
View Related
Jul 10, 2014
I am currently developing a sync module using asp.net. I am getting data from an external database (mysql database) The problem is I am getting the error "Connection unexpectedly terminated" when filling the dataset.
Here's my code
string P_contact = "SELECT A.id, B.firstname, B.middlename, B.lastname FROM Accounts A, Contacts B WHERE A.id = B.id";
MySqlDataAdapter db_P_contact = new MySqlDataAdapter(P_contact, conn);
DataSet ds3 = new DataSet();
DataTable dt3 = new DataTable();
[Code] ....
I am getting the exception on this line of code:
db_P_contact.Fill(ds3, "P_contact");
View 3 Replies
View Related
Feb 6, 2013
I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.
Code:
#include <stdio.h>
int main(void)
{
int sum = 0, temp = 1;
}
[code]...
View 4 Replies
View Related
Jan 19, 2014
for (int raw1=0;raw1<4;raw1++){
for (int column2=0;column2<4;column2++){
unsigned char temp=0x00;
for (column1=0;column1<4;column1++){
int z1=7;
int p1[8]={00000000};
unsigned char temp2;
[Code] ....
When I print the variable output_mix_column inside the for loop as shown in the code, it prints the right output, but when I print it outside the for loop, it changes its value ....
View 1 Replies
View Related
Aug 1, 2013
Code:
#include <stdio.h>
int main(void){
int a=0;
for(;a<=10;)
int b;
return 0;
}
I have got a code like this. I don't expect to get an output but just assumed I would see the command screen until I terminated it. What I want to do is just declare a variable b in a endless loop. But what I got from the compiler is this error: error: expected expression before 'int'. I am using Code::Blocks and I think the compiler is GCC.
View 4 Replies
View Related
Apr 28, 2013
I have a problem with my code which I can't work out:
double Mi = 200*pow(10,30);
cout << "
Enter accreted mass increment in solar masses
";
cin >> dm;
cout << "
[Code] ....
Basically the loop works, but gives the wrong results.
I need, at the end of the loop, to sort of "redefine" Mi as "Mi + Macc". I then need it to repeat the loop, and at the end add another Macc so that Mi becomes "Mi + Macc + Macc", etc.
View 5 Replies
View Related
Mar 25, 2013
This code is used in scientific calculation for optimization problem.
Basically a particle is moving in a three dimensional space, its position is (x,y,z).
At each position, there is a fitness value associated with that position.
The fitness value is given by fitness(x,y,z) (code line 12~19).
We need to find out, when the particle moves around randomly, what is the highest possible fitness value.
To solve this, below code is used, and it produces correct result.
#define DIMENSION 3
#define MAXX 4
#define MINX 0
#define MESHsize 1
#include <iostream>
using namespace std;
float maxValue = 0.0;
[Code] ....
The output of the code:
[ fitness(0,0,0) = 0] [ fitness(0,0,1) = 1] [ fitness(0,0,2) = 4] [ fitness(0,0,3) = 9]
[ fitness(0,1,0) = 1] [ fitness(0,1,1) = 2] [ fitness(0,1,2) = 5] [ fitness(0,1,3) = 10]
[ fitness(0,2,0) = 4] [ fitness(0,2,1) = 5] [ fitness(0,2,2) = 8] [ fitness(0,2,3) = 13]
[ fitness(0,3,0) = 9] [ fitness(0,3,1) = 10] [ fitness(0,3,2) = 13] [ fitness(0,3,3) = 18]
..... so on
Answer: highest fitness = 27
Note: In this case, the values of x,y and z is integers from 0 to 3 inclusive.
For 3-dimensional space above, actually the code had run through 3 nested "for" loops.
Question: Above code works for 3-dimensional space. How to generalize the code, so that it works also for N-dimensional space, where N is an arbitrary integer?
( Note: possibly N = 30 )
View 19 Replies
View Related
Feb 7, 2015
I want to create a loop that that generates a random number set and gives the user the option to run the loop again for a new number. I have that working but when the loop ends the variable value does not leave the loop. I tried a posttest and pretest loop.
Is it possible to declare/create/alter a variable in a loop and have that value exit the loop to use in the code later? I am still learning about global variables.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
int oneDiceSix() // Roll 1d6
{int oneDiceSix = 1+(rand()%6);
return oneDiceSix;} //end function
[code]....
View 2 Replies
View Related
Apr 4, 2013
I've been messing around with loops/functions and basic logic and come up with a small maths program. Here it is:
Code: #include <iostream>
#include <string>
float divide (float x, float y) //function to divide numbers {
return x / y;
[Code] ....
Would initializing the string 'anotherGo' to a value that makes the loop run at least once be a suitable way of doing this rather than using a do/while loop? I read that a do/while loop is a black sheep but I have come across a number of uses for it. Maybe it is just preference which one you should/could use?
View 9 Replies
View Related
Sep 3, 2013
If a variable is declared in a block or for loop block, we all know the scope of a variable are usually in that block. But how about life span?
for example the following code
while(condition) {
....
for(;;) {
int i;
....
}
...
}
if we repeat while loop, every time when we get into the for loop, an i will created and used? but what about the old i? Is is destroyed? I am worrying about the memory will be used up if I do this.
View 6 Replies
View Related
Jan 19, 2015
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
float rate, hours, overtime, tax_calculated;
float tax = .15;
[Code] ....
View 7 Replies
View Related
Feb 20, 2014
After searching, it seems that I've finally arrived at the good old traditional "Random Number Game". I've been presented with solving this problem in the context of Visual C#.
I've solved the first part of the problem: Have the user enter a random number, display "Too high" or "Too Low" depending on the entry. If user guesses, let the user know. That part of the problem I was able to solve with only 3 IF statements. In it's current version, instead of using all IF statements, I saw where 3 test conditional weren't needed. (If it's not greater nor lesser it's equal or,.... "otherwise do this").
An enhancement to the problem ask that the user be notified of how many guesses it took to get the number correct. I reasoned that number of guesses could be translated to mean " number of iterations ".
I'm having trouble using the counter variable to track the iterations, then display the total iterations. I'm not seeing how to increment that value with each loop. I've basically reached two results no matter how I've used counter and written the while loop. The first one returns back to me "the guessed number". Example, if I guessed 10 times and the lucky number was 58, the display statement in the ELSE clause became: "Congrats. You guessed the number in 58 tries!".
The other result is what's returned by the code in it's current form. I think it'll be clearer for me to debug from this point then the previous. Here, I can see how I reach "Congrats. You guessed the number in 2 tries!". It's apparent that counter is being incremented by one regardless how many guesses the user makes. On the contrary, it's not so apparent to me how previously, the value returned by the counter variable at the end of the loop was actually the value for the random number generated and the user's entry (hence, the correct number) and NOT the total loop iterations.
Here's the code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
[Code] .....
View 4 Replies
View Related
Nov 15, 2013
I have an exam in C programming I'm a little stuck on loops..I have to make a program that multiplies the loop variable by 2 then takes away 1... easy, that was a for loop no problem
for (x=3; x<=65; x=x*2-1)
that should print out 3 5 9 17 33 65 however, my lecturer has given the task for it to reverse, but not to display the number 17 on the way back down using an if statement...however, this time it must be a do while loop... This is what I have so far.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 129;
}
[code]....
View 7 Replies
View Related
Sep 10, 2013
What is difference (memory allocation or any) between declaring a variable inside or outside the variable
program1:
#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {
[Code] .....
Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.
View 1 Replies
View Related
Aug 11, 2014
I'm writing a delete function for a linked list, and I'm having issues with this bit of code:
void deleteNode(int data){
node* del = NULL;
t = h;
n = h;
while(n != NULL && n->_data != data){
t = n;
n = n->next;
}
}
Or more precisely, this portion:
&& n->_data != data
n is my new node variable, _data is the storage variable in the private section of my class, and data is the information being searched for that needs to be deleted. Everything works without this section of the code. My assumption is that n->_data is somehow wrong, but I don't see how. I've tried everything I can think of- using parenthesis, using the variable rather than the pointer, I've tried expressing the pointer in a different way, I've tried using my t variable rather than n, and I've found examples online that use this exact same expression without any issues.
View 2 Replies
View Related
Feb 3, 2013
I got the following lines of code from a book. The variable char c is being used here to demonstration local variable declaration.
while(char c = cin.get() != 'q') {
cout << c << " wasn't it" << endl;
if(char x = c == 'a' || c == 'b')
cout << "You typed a or b" << endl;
else
cout << "You typed " << x << endl;
}
It compiles and runs. There are two issues when I try to run it.
1) It seems to loop through two times for every entry. If I insert cin.ignore() just before the closing bracket, it seems to work better.
2) the variable c does not seem to have the character I entered when examined in the if statement.
What is happening with the variable c inside the while loop scope?
Does c actually get initialized?
View 2 Replies
View Related
Feb 20, 2015
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.
View 8 Replies
View Related
Jan 14, 2014
I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.
#include "stdafx.h"
#include<iostream>
struct node{
int data;
node *next;
[Code] .....
View 1 Replies
View Related
Aug 3, 2014
So I have been given and as part of the solution I need to count the number of digits in a long long variable. To do this I use a while loop, but it is behaving strangely. Here is the code.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main (void) {
printf("What is the card number?");
long long card = GetLongLong();
if(card <= 0)
[Code] .....
When I execute the program it asked for the number, but then nothing happens. Of course, my first instinct was that the program was caught in an infinite loop somehow, but could not figure out how. I commented out the while loop and the program completed (albeit returning the incorrect value), so I was further convinced that there was an infinite loop that I was not seeing. I ran the program throug gdb. Strangely, the program did not loop infinitely, instead it got to line 16 [while(card1 > 0] and then just stopped, it was not executing the next line of code at all.
View 6 Replies
View Related
Mar 20, 2013
I need to create a loop to return a minimum value using a boolean variable but don't quite understand how to format my algorithm.
#include <iostream>
using namespace std;
int main() {
bool first = true;
while (first) {
cout << "Enter a number (X to exit): ";
int num;
[Code]....
I know its jacked up any comments in the code would be great I need to finish the program but also understand it.
View 6 Replies
View Related