Visual C++ :: Do Other Things Whilst In A Loop
Mar 25, 2013
I'm writing a piece of software in MFC. In one function, I have a while loop using "KeepLooping" as the condition.
"KeepLooping" is a boolean that is set to false by a toolbar button, key press or menu option. The problem is, when the loop starts running, its obviously so busy in the loop that button presses, toolbar buttons and menu clicks do nothing, so the loop never exits.
What's the best (or should that be simplest?) way around this? I remember in VB you could rather crudely just use DoEvents in a loop to make sure it carried on doing other things in the mean time, but I'm guessing its not that simple in C++.
View 13 Replies
ADVERTISEMENT
Apr 21, 2014
I have the following code. however, when I debug it gives an error saying" vector subscript out of range"
Vector based mufti-dimensional arrays
Vectors are a STL container that allow you to store pretty much anything in them. When used correctly they can be very powerful containers.
They provide an added benefit that they will automatically remove the memory they use when they go out of scope. This means that objects stored within a vector do not need to be de-allocated (but pointers to objects do). You can also do some interesting things with dynamic multidimensional arrays with vectors.
For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd dimension it's no longer a grid, but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors).
This functionality can be achieved using pointers, but is much harder to do.
#include <iostream>
#include <vector>
#include<conio.h>
using std::vector;
using namespace std;
[code]...
View 8 Replies
View Related
Oct 28, 2013
So I started taking a C++ class two weeks ago and we just got to arrays. I am confused on how to pull data from two dimensional arrays. The assignment for this topic reads as follows. Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a. Function getData: This function reads and stores data in the two dimensional array.
b. Function averageHigh: This function calculates and returns the average high temperature for the year.
c. Function averageLow: This function calculates and returns the average low temperature for the year.
d. Function indexHighTemp: this function returns the index of the highest high temperature in the array.
e. Function indexLowTemp: this function returns the index of the lowest low temperature in the array.
I made the getData function. I do know that i am supposed to more then likely use nested loops on each function to iterate through each column. I have been trying to figure this out. I first saw in my book that the array could be variables so naturally I tryed to make the array loop and increment up the column and row each time a item is stored to make it easier to loop in different functions but then i found out that they have to have a constant value and that doesn't work. Here is the code for index high temp that i have been working on. I honestly feel like i'm over thinking this and its easier then what i'm making it out to be. Here is all of the code.
Code:
#include <iostream>
#include <string>
using namespace std;
void getData(int month);
void averageHigh(int month);
void averageLow(int month);
[Code]...
View 4 Replies
View Related
Feb 20, 2013
I've been working on this program and am finished with what I want it to accomplish. But the last step I'd like to take is to delete the linked list and free all the memory. When I free the memory the way I am right now, the program breaks. I'm not sure how I should go about freeing and setting things to NULL.
Code:
typedef struct node {
char* value;
struct node* next;
} LinkedList;
//function prototypes
void llAdd(LinkedList** ll, char* value);
[Code] .....
View 6 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
Jul 12, 2014
How would I make a loop that would simultaneously compute both the max and min of a vector?
View 2 Replies
View Related
Sep 28, 2014
You place a breakpoint at one of locations within the message loop. But I want the breakpoint to trigger when I click on the application window, for example. But I can't, it went so fast so long as I switch from VS to the application program, the breakpoint is hitted again, then the application is frozen again. I don't know how to setup a conditional breakpoint.
View 3 Replies
View Related
Jul 28, 2013
I'm trying to put an if-else statement in a do-while loop and when I put the correct answer in the if statement I get that error. Here's the program:
#include <iostream>
using namespace std;
int main() {
int answer;
char repeat;
[Code] .....
View 5 Replies
View Related
Oct 18, 2014
I just started programming and want to make the game cows and bulls. The problem is that after the loop has ended and both vectors are not the same I want the user to 're-input' the starting variables a,b,c&d but I don't know how to.
Code:
#include "std_lib_facilities.h"
int comparison(int a, int b, int c, int d)
{
vector<int>bc={ 1, 2, 4, 9 };
[Code].....
View 2 Replies
View Related
Jun 16, 2013
I have a VC++ 6 dialog-based application. This application is intended for reading data from a USB device (this is a FTDI FT245R chip). I have a start button on the dialog. On clicking it, unsigned char data retrieved from the USB device is displayed on the dialog. The start button invokes a function which includes a while (1) loop inside which the USB device is read and the values displayed.
There are no break or continue statements inside the always operating while (1) loop. All this works fine. My problem is how do I exit the application as normally it would just run forever? I have put an exit button on the dialog with code added to call OnOK(). Normally this would nicely close the application but as I have an endless while loop in my application so this button doesn't seem to work. Nothing happens on clicking it and clicking it repeatedly just hangs the program.
View 8 Replies
View Related
Jan 23, 2013
Code:
while(!pile.eof()){
pile.getline(inputbuff,199,'
');
count++;
[Code] ....
the loop iterates the first time through qualifying for (count % 2 != 0) because count is at 1 so it stores the characters after the comma into tok and does it successfully according to my debugger.then the while itterates again because it doesn't qualify for the next if. this time it does qualify for count % 2 == 0 but my tok pointer is now null , how can that be? does it go null when the while loop itterates for the second time? that shouldn't be because my integer count doesn't reset?
View 4 Replies
View Related
May 15, 2013
I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.
#include <iostream>
#include <cmath>
using namespace std;
double original_cost;
double discount;
double tax;
double total;
char answer;
[Code]...
View 2 Replies
View Related
Jan 20, 2014
So I have this problem with not being able to iterate over a vector in a nested for-loop. Here's the nested for-loop:
bool innerHit = false;
for (std::vector<Sprite*>::iterator outerIter = sprites.begin(); outerIter != sprites.end() && (!sprites.empty()); outerIter++) {
Sprite* spriteOne = *outerIter;
for (std::vector<Sprite*>::reverse_iterator innerIter = sprites.rbegin(); innerIter != sprites.rend() && (!sprites.empty()); innerIter++) {
Sprite* spriteTwo = *innerIter;
[Code] .....
What happens is, after having called the collisionDestroy-function and the program tries to execute the nest loop in the outer for-loop, it all crashes with the text "Expression: vector iterator not decrementable", which I understand is because the iterator will have already become useless. The question is: know this, how do I fix it? I can't seem to get a hang of it.
Here's the collisionDestroy-function (the collisionReaction does nothing but sets a few local variables):
void Enemy::collisionDestroy(std::vector<Sprite*>& sprites) {
for (std::vector<Sprite*>::iterator iter = sprites.begin(); iter != sprites.end(); iter++) {
Enemy* tmp = dynamic_cast<Enemy*>(*iter);
if (this == tmp && collisionType == 3 || collisionType == 1) {
sprites.erase(iter);
break;
}
}
}
View 14 Replies
View Related
Feb 28, 2013
I am stuck on an exercise where i am supposed to use a loop to take user input and keep a running sum until the user enters a 0. the code i have so far is:
#include <iostream>
int main() {
using namespace std;
int num;
int total = 0;
int x;
[Code] ....
The full text of the error message is: error c2678:binary'>>':no operator found which takes a left-hand operand of type 'std::istream' . and one more thing i was wondering, is there a difference between c++ and visual c++?
View 4 Replies
View Related
May 7, 2014
I'm programming an app that deals with large numbers. I have a do-while loop that I want to execute. I can not get it to work. The "while(d!=1)" part is the problem and I can not find a way around.
Code:
#include "stdafx.h"
#include <stdio.h>
#include <gmp.h>
#pragma comment(lib, "gmp.lib")
int main (int argc, char *argv[]) {
mpz_t d;
[Code] ....
I get errors:
1.error C2446: '!=' : no conversion from 'const int' to '__mpz_struct *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
2. '!=' : '__mpz_struct [1]' differs in levels of indirection from 'const int'
View 1 Replies
View Related
Nov 28, 2014
I am trying to make a basic app that will loop through an unordered list with repeats and count how many times a specific item repeats. Example would be I select a state from the list box and it will tell me how many times it is listed.
This is the code I have up so far, trying to keep it basic. I am missing something, something related to the counter? I think I have some of the better half up but I am not sure...
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstBox.SelectedIndexChanged
Dim wins As String = lstBox.SelectedIndex
Dim foundwins As Boolean = False
Dim i As Integer = -1
[Code] ....
View 2 Replies
View Related
Sep 28, 2014
I'm new to programming and i'm trying to do a certain task. I want to use a for loop to read certain data from a txt file and print them to a console. I'm trying to read student names and their grades.
Something like
3 // 3 represents the number of students.
George 97
Sarah 70
Maya 88
The data may vary but it's always in this format.
View 14 Replies
View Related
Jul 14, 2013
I am trying to create a loop to call in the entries from the text file named Set. The entries of Set.txtare :
1 2 3
2 4 5
and so on .. (64 such combinations)[/CODE]
It basically means the first combination for testing is 1 2 3 and next has to be 2 4 5 and so i have 64 such entries defined in set
My test files are located in D://data// and are named tst_data1 to tst_data64.
I created a loop for test set but its incorrect
Code:
// loop for test samples
char basefilename[] = "D://data//";
char , testFilen[160], numiChar[10];
for (int i=1; i<=64; i++) {
strcpy(basefilenme, "D://data//");
strcat(testfilename, Set[]);
[Code] .....
How can i call the Set .txt and how to define it.
View 1 Replies
View Related
Aug 25, 2013
I am writing a program to auto login in a web site. Before making next attempt I have to wait for some time like some 10 seconds(it is configurable). But during waiting UI should not freeze. I wrote code something like this. Here event m_hEvent[1]) will be set by another thread after 10 seconds.The problem is UI still freezes some times!
while(1)
{
//m_hEvent[1] will be set by another thread after 10 seconds
dwRet = MsgWaitForMultipleObjects(1, &m_hEvent[1], FALSE, dwMilliseconds, QS_ALLINPUT);
ResetEvent(m_hEvent[1]);
[Code] ....
View 4 Replies
View Related
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
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
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
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
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
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
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