C++ :: Make A Loop Inside A Condition?
Oct 26, 2013
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
};
View 7 Replies
ADVERTISEMENT
Oct 6, 2014
I've written a bit of code :
#include <iostream>
#include <cmath>
#include <cstdio>
int main() {
for(int i=1; i<=1000; i++)
for(int j=i+1; j<=1000; j++)
for(int k=j+1; k<=1000; k++)
[Code] ...
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.
View 4 Replies
View Related
May 23, 2014
I found a for loop in an example that I don't understund fully.
Code:
for (i=64; i; i/=2)
printf("i: %d
",i);
Now this is dividing by 2 until it reach '1' and stops. But why does this stops?
View 6 Replies
View Related
Dec 8, 2014
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,'
');
}
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
May 18, 2013
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?
View 11 Replies
View Related
Mar 21, 2015
[URL] ....
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)
[Code] ....
View 1 Replies
View Related
Apr 2, 2014
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')
[Code] .....
View 9 Replies
View Related
Aug 5, 2013
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.
View 1 Replies
View Related
Mar 8, 2013
Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;
[Code] ....
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?
View 8 Replies
View Related
Nov 15, 2013
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.
View 1 Replies
View Related
Aug 25, 2014
I know that if I erase a value inside an iterator, it becomes invalidated, and this is how you would normally fix it:
for(std::vector<int>::iterator it = list.begin(); it<list.end(); ) {
if((*it) == 5)
list.erase(it);
else
it++;
}
But what about a double loop? like this one:
This would delete all duplicates
for(std::vector<int>::iterator it = list.begin(); it<list.end(); it++) {
for(std::vector<int>::iterator it2 = list.begin(); it2<list.end(); it2++) {
if((*it) == (*it2))
list.erase(it2);
}
}
View 6 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
Feb 22, 2013
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');
[Code] ....
View 3 Replies
View Related
Nov 25, 2014
I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....
Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;
[Code] ....
View 6 Replies
View Related
May 27, 2014
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;
[Code] .....
View 4 Replies
View Related
Dec 17, 2013
it seems everytime i use statics in a class i come across with a porblem.
this time i wanted to make a class i created static inside another class.
MainVariables.h file
static fge::window mWinMain;
if someone ever wants to reach it
MainVariables.cpp file
fge::window MainVariables::mWinMain;
...
...
fge::window MainVariables::GetWinMain()
{
return mWinMain;
}
but when i created some other MainVariables classes at other places instead of them reaching to the same window two window is being created.
yes i know maybe there are better methods but currently i m working on polymorphism and i need some static members.
View 2 Replies
View Related
Mar 3, 2014
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;
[Code].....
View 2 Replies
View Related
Nov 20, 2014
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.
View 4 Replies
View Related
Aug 29, 2014
How to make a loop so program never end, it end only by typing "EXIT"
View 1 Replies
View Related
Dec 9, 2014
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;
} }
View 2 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
Apr 4, 2013
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
[Code] ....
View 8 Replies
View Related
Dec 11, 2013
Something that does something like this
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.
View 1 Replies
View Related
Dec 11, 2013
[URL] ....
This is my code, but how can I implement this?
View 1 Replies
View Related
Dec 31, 2013
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?
View 3 Replies
View Related