C++ :: Initialize A Variable In Order To Run Loop At Least Once

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


ADVERTISEMENT

C++ :: Initialize A Matrix In While-loop?

Jun 21, 2013

I have written a program, in which I create a NxN-matrix. As I need some different samples (some different such NxN-matrices), I try to create a new matrix in each recall of a while-loop. The first recall of the while-loop works without any problems, but the second recall crashes, but I get any errors from the compiler. Here is my quell code. The problem should be with line 44:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

[Code].....

View 5 Replies View Related

C :: How To Initialize A Variable That Is Not A Letter Or Number

Feb 25, 2014

In C how can I initialize a variable that is not a letter or number? For example with a number I can :

Code:

int i = 5;
for ( i = 0; i <=5; i++ );
printf( "%d", i ) This would display a row of 5's

but what if I wanted to display a row of -----? What I am trying to do is read in a simple txt file, around the file I want ----1----2-----3 ect ect on the top ----a----b-----c down the side Then I want to be able to change the file at lets say position c2 and save it. This is the early stages of my attempt to set up a editable table.

View 3 Replies View Related

C :: Initialize For Loop Using Input From Keyboard?

Feb 22, 2015

I'm not finished with this code, but I am stuck on this one glitch. This is what I have so far, and it's not working.

Code:
int main() {
/*Please input an n value greater than zero. Otherwise, exit the program by entering a carriage return*/
printf("Please input an n value greater than zero. Otherwise, exit the program by entering a carriage return
");
int summation = 0, x, y;
scanf("%d", y);
for (y == x; summation <= M_E && x <= 34; x++)

[Code] ....

View 1 Replies View Related

C :: Initialize A Pointer Variable With A Knowing Address?

Dec 28, 2013

I need to initialize a pointer variable with a knowing address. See code below, ptr is the final destination and value of ptr_address contains the address value, so I need to do something like ptr = *ptr_address.

Code:

int *ptr;
int *ptr_address;
int address;
address = 0x10000005;
ptr_address = &(address);
ptr = *ptr_address;

The problem is that compiler gives the following warning message:

warning: assignment makes pointer from integer without a cast [enabled by default]

Is my code wrong or there is any other way to do it without receiving this compiler warning?

View 3 Replies View Related

C/C++ :: Declare And Initialize A Pointer To Variable Int Cost

Mar 2, 2014

this question would their be a different process if they asked "declare and initialize a pointer to the variable int cost[N][N]" Here's what I have so far

[#include<stdio.h>
int main() {
int a; // Step 1
int *ptr; // Step 2
a = cost; // Step 3
ptr = &a; // Step 4
return(0);

[Code] .....

View 5 Replies View Related

C :: How To Initialize Variable Word In Separate Statement From Its Declaration

Aug 7, 2013

I wrote the following program to initialize a string after the variables is declared, but it isn't working. A warning is given by the compiler, and the execution of the program shows a strange string. How do I initialize variable word in a separate statement from its declaration?

Code:

#include <stdio.h>
int main( void )
{
char word[20];
*word = "Hello";

printf( "The string is %s.
", word );
return 0;
}

View 3 Replies View Related

C/C++ :: Printing Table Of ASCII Chars - Cannot Initialize Variable

Apr 9, 2014

I'm trying to write a program which prints a table of ASCII chars, I'm not really done with my thoughts on it but I already
ran into the following error:

Error: cannot initialize a variable of type 'char' with an rvalue of type 'char (*)[16]'

Here's my code so far:

# include <iostream>
using namespace std;
int main() {
char asciiTable = new char[8][16];
int nextSign = 0;

[Code] .....

View 4 Replies View Related

C++ :: Write A Loop Assigning Variable X To All Positions Of String Variable

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

C/C++ :: Not Able To Initialize Structure Variable Inside Main Whose Structure Defined GL

Aug 27, 2013

I am trying to run a programme implementing a function with structures in c... which is:

#include<stdio.h>
#include<conio.h>
struct store  {
        char name[20];
        float price;    
        int quantity;

[Code] .....

View 1 Replies View Related

C/C++ :: Loop To Add Ints / Strings Into Vector In Ascending Order

Feb 24, 2014

The code is supposed to take either an int or a string (and their respective vectors) and insert a given int or string into the vector in ascending order. My code works properly for ints, but it's having a problem with strings.

The order I get with the strings given is

penguin
banana
great
jungle

For some reason comparing penguin to banana/great doesn't give the expected result. The template attached only includes the function and the private vectors needed for the function.

template<class T>
class orderedList {
public:
void insert(const T& item);
private:
vector<T> list;
int total = 0;

[Code] ....

View 11 Replies View Related

C :: Loop Statement To Print Numbers From 1 To 10 In Reverse Order Separated By Star

Apr 21, 2013

Write a for loop statement to print the numbers from 1 to 10 in reverse order separated by star :

10*9*8*7*6*5*4*3*2*1

i try to do it but it show me like this :

10*9*8*7*6*5*4*3*2*1*

how do i write to show me like the first one ?

View 10 Replies View Related

C :: Variable In While Loop

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

C/C++ :: Value Of Variable Changes After The End Of For Loop

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

C :: Declaring Variable In A For Loop

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

C++ :: Changing Variable In For Loop

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

C++ :: Variable Nested For Loop

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

C++ :: Variable Stuck In A Loop?

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

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

C++ :: Life Span Of Variable - While Loop?

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

C/C++ :: Skips Loop Even Though Variable Is Not Negative

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

C# :: How To Correctly Use Counter Variable In WHILE Loop

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

C :: Make A Program That Multiplies Loop Variable By 2

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

C++ :: Difference Between Variable Declaration Inside And Outside For Loop

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

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/C++ :: Local Variable Declaration - While Loop Expression

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







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