I need to do make a loop inside a condition. Can it be done? I don't want to call another function to do it. Any way at all without calling separate function inside the if? I just want to do:
if (
for (int i = 0; i<=10; i++)
{
//stuff related to the for loop
} )
{
//stuff related to the initial if condition
};
Now why does defining the if condition as if(pow(i,2) + pow(j,2) == pow(k,2)) doesnt work (ie. doesn't print anything) while defining it as if(i*i + j*j == k*k) works flawlessly - by working I mean printing out single set of 3 numbers.
The while loop part of my program isn't working right. stringOriginal is an array. If a large amount of characters passed the max 80 im processing is typed, it goes into a infinite loop. Also the condition doesn't work i type in quit and the loop continues.
while(stringOriginal != "quit"){ std::cout << "Enter a string of characters you would like to reverse "; cin.getline(stringOriginal,79,' '); }
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.
Write a program to calculate the Loan Balance, where a person borrows an amount A and in return he/she agrees to make N payments per year, each of amount P. While the person is repaying the loan, interest will accumulate at an annual percentage rate of R, and this interest will be compounded N times a year (along with each payment). Therefore, the person must continue paying these installments of amount P until the original amount and any accumulated interest is repaid.
NOTE: The formula to calculate the amount that the person needs to repay after T years is-
Balance Amount after T years = A[(1+R/N)^NT]-P -----------------------------------------------------------
I have a few doubts :
1. I think that the "balance amount" formula can directly give the "loan balance" for the person. I'm not sure if it's correct but in that case the question would serve no purpose. Maybe I'm wrong.
2. If there should be a loop to calculate the loan balance, what condition should I give and which loop will be better to use?
Can I make these squares appear around the other instead of having them appear under the others. Also if it can be done how can I make it so there is a space between the squares
int main() { char a; int size = 0; cout << "Enter a character: " << endl; cin >> a; while (size < 1 || size > 10)
I'm trying to create a simple calc program that does all the elementary calculations. How to get it to add and multiply continuously without crashing. but now i need to figure out how to ask the user if they want to continue or not. I'm having trouble here because in the while statement i have a scanf which asks for the operator symbol. and then asks for number in the second scanf..
So basically a user would have to enter 'R' twice for the message to pop up! Also i'm not sure how i would quite the program if they put in a 'N' for no. would return 0; work? What if they press 'Y' for yes, what would the return have to be then?
Here's a snippet of the code int main () { double result; double new_number; char symbol; result = 0; while(1==scanf(" %c", &symbol) && symbol == 'R' && symbol =='r')
I have two files like original.txt and replace.txt which has equal nbr of lines in both of them.
I need to loop through these two files line by line and replace the line in original.txt to the line in replace.txt
eg :
output.txt :
1|raj|65.4|Y| 1|ramesh|65.4|Y|
replace.txt :
1|raj|65.4|Y| Cannot be processed|1|ramesh|65.4|Y|
What I need here is when reading of output.txt reaches second line of file "1|ramesh|65.4|Y|", it has to be replaced with the second line in replace.txt "Cannot be processed|1|ramesh|65.4|Y|".
1|ramesh|65.4|Y| --> Cannot be processed|1|ramesh|65.4|Y|
After the end of loop the contents of two files should be like :
original.txt :
1|raj|65.4|Y| Cannot be processed|1|ramesh|65.4|Y|
replace.txt :
1|raj|65.4|Y| Cannot be processed|1|ramesh|65.4|Y|
The files can have variable number of lines but both will have same number of lines each.
I'm confused with this last [for] loop; How is ptr++ applied for non-integer value? Ptr is clearly a char, it comes from str, which reads string line from file.
I come from C# background, I have never met for...loop which irritates by using [somechar]++, not [someinteger]++; What is actually going on there?
Some other similar example might be:
Code: (iColor+(_parts[j]%length)*3),
where iColor is static unsigned char iColor[] array;
I would expect to see iColor[somevalue] + (_parts[j]%length)*3), but here in C++ I sometimes see that integer is being added directly to the array. What does it mean, what happens?
I've been working on a program on and off for around a week now and I've been struggling towards the end of the program.First of all, the program is a maths quiz which generates two random numbers per question.I'll give you one part of my code:
Code:
srand ( time(NULL) ); //seeds the random number generator int score = 0; int a = rand()%12 +1; //generates a random num between 1-12 int b = rand()%12 +1; int c = a+b; int d; }
[code]....
I've basically copied the above code 10 times and changed the variables by going through the alphabet e.g.
Code:
int a = rand()%12 +1; //generates a random num between 1-12 int b = rand()%12 +1; int c = a+b; int d; all the way to
Code:
int an = rand()%12 +1; int ao = rand()%12 +1; int ap = rand()%12 +1; int aq = an+ao-ap; int ar;
Now what I'm going to do is remove all the declared variables and create a loop. But my problem is; If I wanted to declare four variables for e.g.
Code:
int a = rand()%12 +1; int b = rand()%12 +1; int c = rand()%12 +1; int d = a+b-c;
Would I place the srand( time(NULL)); inside the loop? it's confusing because I know an example of a basic loop with an array would be:
Code:
#include <stdio.h> #include <conio.h> int main(void) {
int test[5]={21,18,47,21,4}; int I; int total=0;
for (I=0;i<5;i++) total += test[I]; }
[code]....
how or where to include the random number generator in the loop and to make it ask 10 questions at random.
I am writing a program where all work is done in the class methods. Main is used to call the methods. I need to know how to get a loop to work without any variables in main that can be used outside the methods. This is what I have in main:
#include <iostream> #include <string> using namespace std; #include "FerryBoat.h" int main() { //create a constructor for a ferry boat FerryBoat myBoat('B', 20, 'A');
I am making a program to run Fibonacci series. I have created 2 array.
1)- 1st array holds only 0, 1 2)- 2nd array holds other values eg: 1, 2, 3, 5..........etc
I am using while loop to get the febonacci series and storing it in a 2nd array called int[] numbers.
After getting value using while loop, I am joing both the arrays using int[] final = arr.Concat(number).ToArray();
At last, I have used foreach loop to add the febonacci series into the listbox.
The problem I have is that, I cannot able to concat both the arrays. I tried to assign number array at the top of the while loop. so that number variable will be accessible outside the while loop. But I am getting a error.
See the code below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
I'm currently reading/programming from "Head first C#" book. I have this project which is making a program that has four dogs which race and there are three players that can bet on the race. I need to learn how to make the "dogs" (picture boxes) move from left end to the right end of a picture box.
Here is the code:
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing;
I'm trying to write a program for a lab, I know how easy it is but cant see to crack it.
I need to make a program using while or for loop.
I must have the user type a number, for an input value an output value but then do it again several times but without it going on forever asking again and again.
I'm trying to optimize this code using openMP. The line I added made it run about twice as fast but I'm trying to figure out how to make it even faster. Could reshaping the loops potentially increase the speed?
//4 threads int listsize=15000; #pragma omp parallel for shared(f) private(i,j,hash) for(i = 0; i < listsize; i++) { printf("Thread: %d i: %zu wl_size: %zu ",omp_get_thread_num(),i,wl_size); for (j = 0; j < num; j++) { h = f[j] (getw(list, i)); c[h] = 1; } }
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 making a program for a MMO I play. It calculates the cash and resources needed to build certain 'modules'. I'm having trouble to do something very simple : make a loop until the user asks to break the loop. Here is what the interface looks like :
What module do you want to install?
(Examples: 'Avenger' or 'VLLT')
dd // I input this DD. COST each : 60,000 cr.
How many do you want? (Modules left:15) (NOTE : You may enter a negative number if you made a mistake) 3 // I input this Current Total Module Price : 180000 cr +6 droids
[Code] ....
The bigger code box below is a part of the code I made. What I deleted is uninteresting, it's just the same thing again : other resources, other 'modules'...
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { struct resources// Defines the amount of resources and their price
at this point run a loop for all players, in this loop use random to determine the face and depending on the face run a loop (for roll again etc) or give next user the chance.
And this loop is 2-12 people, all rolling die. They draw 3 die out of a cup of 12. Then they roll the 3 die, getting fish, hook, or boot on each die.
I'm working on a code that needs to loop a switch case back to the beginning after a certain input is entered.
ex) Code:
printf("Select an option); printf("1. Play game"); printf("2. Status"); printf("3. Exit"); scanf("%i", &userinput); switch(userinput);
[Code]...
For my program, I want option 2 to display something, then loop back to "select and option" after the user presses enter. How would I write that? Would i use a while? do while?