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
how to ask for the input (className) without getting stuck in the loop. When I remove
char className; cout << "Enter the class name: " << endl; cin >> className;
it works fine. When I leave it how it's displayed below it goes in an infinite loop. By the way I think "char" is not the appropriate way to declare className. If I want to output it later, should I changed it to "string"?
#include <iostream> using namespace std; int main() {
my code seems to enter an infinite loop should a user mistakenly enter a character other than a digit. The code is about selecting from a preset number of options, numbered 1- 4. If the user mistakenly enters a letter for example, instead of a number, the infinite loop kicks in...
Code: #include<stdlib.h>#include<stdio.h> #include<unistd.h> #include<math.h> int main(void) { double N, NG, LG, epsilon, root; // setting all variables to type double
[Code] .....
The goal is to create a program to calculate the square root of a number provided by the user to an error tolerance 0.005
Looking around i fond the Code: fabs(NG - LG) < epsilon); section that was very similar to what i was using, but if this is better im down for that.
My issue, from what i can see, is the updating of the values of LG. if the test for error tolerance fails then LG needs to take on the value of the results of NG.
Now I am not 100% that is the point of failure due to the fact the script does calculate the root properly, but it never exits the program once it reaches the tolerance level.
Code: imac:ENG-3211 user$ ./hw_4_1
Please enter the number you wish to find the square root: 4
I currently have a hangman game in the making. Which is giving me debugging issues when I go to pick a letter, it will keep asking for a letter, if I place a break; within the loop it asks for a letter and says you've won. I know all I should need is a couple extra lines somewhere within the code.
/// Play game public static string playGame() { Words words = new Words();
I'm having trouble getting my loop to work correctly. If I iterate the for loop once it works as expected and displays proper output. When I try to iterate two times or more the program gets stuck in an infinite loop.
I am trying to get this code eventually to read in a maze file to move the smiley face around in. But right now my current snag is the yes or no to enter the for loop.
#include <iostream> #include <windows.h> #include <conio.h> #include <time.h> using namespace std; int main() { int name; char ans;
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.)"
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; }
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 ....
#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.
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.
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?
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?
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';
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.
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;
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; }
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.
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 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;
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.