C++ :: Using Pow (x Y) As Loop Condition

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


ADVERTISEMENT

C :: For Loop - Stop Condition

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

C++ :: While Loop Condition Not Working Correctly

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

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 View Related

C/C++ :: Comparing Pointer To Variable As While Loop Condition

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

C :: Calculating Loan Balance - Finding The Condition For Loop

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

C :: If Statement Won't Check The Second Condition?

Sep 24, 2013

I've tried retyping the code several times and didn't work for some reason the If wont accept both Q and q it just accepts Q.

Code:
#include <stdio.h>
int main(void)
{
printf("A menu will show up and you choose the number for the selection you want.

[Code].....

View 9 Replies View Related

C :: Scanf With Stopping Condition

Mar 22, 2013

I wanted to input some numbers with scanf function, i can enter some numbers and if I input -1 to the scanf, the input must end. And the scanf function has limited input, the max that I can input is 40 numbers.example if enter 1 2 4 6 5 4 -1 the scanf function will ended and the result will be appear.I wanted to know how the scanf function is like that would be best for this problem, Code: scanf("%d", &n); the result if I input those number will be like

|
||
||||
||||||
|||||
||||

View 3 Replies View Related

C++ :: Generalizing Condition In If Statement

Jul 29, 2014

I want to write an if statement which has condition like

if(i%1==0 && i%2==0 && i%3==0 && i%4==0 && i%5==0 &&
i%6==0 && i%7==0 && i%8==0 && i%9==0 && i%10==0 &&
i%11==0 && i%12==0 && i%13==0 && i%14==0 && i%15==0
&& i%16==0 && i%17==0 && i%18==0 && i%19==0 && i%20==0)

This of course is not the right way to write it. How to generalize it so that I can check the condition not only till 20 but till any number without manually adding all conditions.

View 2 Replies View Related

C++ :: If Else Statement With Ctime Condition

Oct 17, 2014

I am try to make a menu for deliver order program.

The menu divide into 3 group which are breakfast, lunch and dinner. However breaker only available at 6.30am-10pm, lunch available at 11am-1.30pm and dinner only available at 6.00pm-8.30pm

Let say my current time on the system is 10.30am, and the user select breakfast order. It should not available at this time, and prompt a message to use that the order not available at this period.

How to write the program?

View 4 Replies View Related

C Sharp :: Condition For Add New Row In Datagridview

Sep 13, 2014

I have two table the first is called Imprrests and the second is called ]IprestsPays. There is an Imprest_ID column that is primary key in [icode]Imprests[/code] table and foreign key in ImprestsPays table

Imprests table consists the following columns:
Imprest_ID,  Impres_value,  Imprest_date,    Employee_ID

Where Employee_ID column is primary key in Employees table and foreign key in Imprests table.

ImprestsPays table consists the following columns:
ID,  Payment_value,  Payment_date,  Imprest_ID

My problem is in the buttonadd_click event in Imprsts form.

I want to prevent user from insert any new imprest for employee in Imprests table if employee haven't paid all payments in ImprestsPays table for old imprest...

View 1 Replies View Related

C++ :: If Function Is Executed Without Its Condition Is True

Mar 5, 2013

I am trying to write a simple program that produces different outputs based on entered age of two different users. Program should tell who is older and behave different if both users are older than 100.

Here is my program: Code: #include <iostream>
using namespace std;
int main()
{

[Code].....

Why program executes this when both users are obviously more than 100

View 8 Replies View Related

C/C++ :: Char Condition In If Statement Not Working

Feb 3, 2015

I'm trying to get an if/else statement to work using a character condition, but it is a no-go. After I input either a 'y' or 'n', the program just sits there until I press 'Enter' again, at which point it ends. It never actually goes through either of the 'if' statements. At first I only had a single '=' in the condition, but I found that that was wrong. When I corrected it to '==' it still didn't work. I also tried clearing the buffer by adding a 'getchar()' at the beginning of the program, but that didn't work either.

#include <stdio.h>
#define std_rate .8855
int main(void) {
char ans;
int counter = 1, euros = 5;
double dollars = 0, ex_rate;

[Code] .....

The compiler (Visual Studio 2013) requires me to use 'scanf_s' instead of just 'scanf' for some reason, and if I don't have two 'getchar()' commands at the end, then it won't stay open long enough for me to see the results.

View 2 Replies View Related

C++ :: Multi Condition Function For Char Type

Jan 30, 2013

I have always written like a>='0'&&a<='9'&&a>='a'&&a<='z' in loops etc, but no more. Basically add whatever you want to condition, and if you want point a to point b just separate them with a '-' sign. Simply

while(!isCharInside(x,"|a-zA-Z0-9.*/=|-|")){/*code*/}
bool isCharInside(char check,string condition="a-zA-Z*/+=|-|0-9"){
for(unsigned int x=0,z=getLenght(condition);x<z;++x){
if(condition[x+1]=='-'&&(condition[x]!='|'&&condition[x+2]!='|')){
if(condition[x]>condition[x+2])swap(condition[x],condition[x+2]);
for(;condition[x]<=condition[x+2];++condition[x]){if(condition[x]==check)return true;};x+=2;}
else if(check==condition[x])return true;
}
return false;}

View 1 Replies View Related

C++ :: Wrong Check For Batch Code Entry End Condition?

Jan 13, 2015

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int check_Pythag(int DIMA, int DIMB, int DIMC) {

[Code] .....

View 4 Replies View Related

C++ :: Parsing A Text File - Detect When Certain Compilation Condition Begins

Dec 8, 2014

I'm parsing a text file, and I'd like to detect when a certain Compilation Condition - i.e. #ifdef - begins. The challenge is, that the condition can take any of the following patterns:

#ifdef (FLAG)
#if defined (FLAG)
#if (defined (FLAG))

(And perhaps I missed more)

I'd of course need to treat them all the same, as they are indeed the same. How would you know to treat them all the same?

View 2 Replies View Related

C++ :: Search And Find The Shortest Queue And Search After Some Condition?

Mar 7, 2013

I am trying to implement a Task scheduler where i have n number of tasks. The Idea behind my task scheduler is that in a loop of queues of a vector, task should get enqueued to the shortest queue among the loop of queues, which is done by the following code.

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
task t // implemented in the other part of the program

[Code] ....

Next i am trying to extend this paradigm to reduce the overhead time of the scheduler, Instead of searching the shortest queue every time, search after some condition ie. search the shortest queue after 5 tasks gets enqueued to the shortest queue.

i need to do something like this

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
task t // implemented in the other part of the program
while(q[min_index].size()!=q[min_index].size()+5) // check whether current min_index queue's size is increased 5 more times if not goto enqueue

[code].....

View 1 Replies View Related

C :: Loop Does Not Work And Program Quits Without Displaying For Loop Function

Oct 13, 2014

We are making a program--but every time we input a value for scanf, the following for loop does not work and the program quits without displaying the for loop's function. We are not getting any errors.

View 11 Replies View Related

C# :: Use Loop To Load Two Forms Back And Forth Then Stop At Loop 4

Feb 15, 2015

I have tried to submit this topic before but i didn't submit my whole code and it was removed. So here it is. All I am trying to do is load form2 from form1 then back to form1 from form2 for a certain number of times the get out of the loop. I am new to C-Sharp and it seems as though I cant seem to figure out a way to do this.

Here is form1 and form2 code. I have commented out a few things I have tried.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

[Code]....

View 3 Replies View Related

C :: Extra Loop Being Executed In Do-while Loop

Jul 1, 2013

Code:
#include <stdio.h>
#include<ctype.h>
void try5t(){

char choice;
int choiceint;

[Code] .....

Loop is repeated an additional time as shown in the screenshot:

View 9 Replies View Related

C++ :: How Much Milliseconds To Loop Through For Loop

Feb 20, 2014

Given a for loop:

int i;
for(i = 0; i < 1000; i++)
;

How many nanoseconds, or microseconds or milliseconds does it take for each iteration? And why do some use it as a timer mechanism?

View 3 Replies View Related

C/C++ :: How To Change For Loop Into Do While Loop

Apr 19, 2014

I already made a nested for loop into a while loop (below this) and now I'm trying to make the outer for loop into a do while loop, but it's not working.

#include <iostream>
using namespace std;
int main () {
int len;
int j;
int i;

[Code]....

And I can't make this code do the same thing. It stops after one loop, instead of continuing to the end. Why won't the loops continue?

#include <iostream>
using namespace std;
int main(){
int len;
cout << "Enter a number: ";

[Code] ....

View 2 Replies View Related

C/C++ :: Convert A While Loop To A For Loop?

Mar 25, 2012

I need to convert the following while loop:

count = 1;
while (caloriesForItem != 0, count != numberOfItems )
{
cout<<"Enter the calories: ";
cin >> caloriesForItem;
totalCalories += caloriesForItem;
count++;
}

This is what I have come up with:

totalCalories += caloriesForItem;
for (count = 1; caloriesForItem != 0; count != numberOfItems)
{
cout<<"Enter the calories: ";
cin >> caloriesForItem;
count++;
cout << "Total calories eaten today = " << totalCalories << endl;
}

However the output is not the same?

View 6 Replies View Related

C# :: Loop Within A Loop?

Sep 25, 2014

I for the life of me can't get my logic straight. I have 2 lists, 1 a list of text, and the other a list of int.

Here's my main list: List<string> ListNeedToADD = ListOfSPSExt.Except(ListOfReportExt).ToList();
Here's my second list:IEnumerable<int> result2 = Enumerable.Range(1, Convert.ToInt32(myValue)).Except(hostList);

I need to loop through my main list, ListNeedToADD, and for each item in that list, grab 1 value from the 2nd list and send those 2 items to a method. Something like this:

foreach (string myString in ListNeedToADD)
{
foreach (var item in result2)
{
intSort = item;
insertReportLimits(attribTable, myString, intSort, con);
}
}

The problem with doing it the way I am above is it loops through ALL the items for each myString.

For my method, it needs to look like this: insertReportLimits(attribTable, "34257", 22, con); then it would move on to the next: insertReportLimits(attribTable, "34854", 27, con); etc etc.

View 4 Replies View Related

C/C++ :: Cannot Get Out Of Loop

Feb 22, 2015

I can't get out of my loop to fill the array. This array should stop and print after 10 inputs. but it keeps asking for input.

This is what my code is supposed to do. Use 0 as sentinel for an array. Only positive ints will be entered into the array. A negative int should result in an error message, then input will continue. The numbers will be inserted into the array in ascending order. Do not insert them then sort the array.

#include <iostream>
#include <cmath>
using namespace std;
const int arraySize =10;

[Code].....

View 4 Replies View Related

C++ :: For Loop To Count Down From 10 To 0

Apr 16, 2013

I am using a for loop to count down from 10 to 0 it's working to count down from 10 to 1 but when the program cames to the 0 then the program freezes by any reason.

Code:
#include <iostream>
using namespace std;
int main()
{
int number[2];
cout << "Enter number: ";
cin >> number[0];
if (number[0] == 1)

[Code] ....

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved