C++ :: Upon Checking Emptiness Of Array - Getting Unexpected Result
May 9, 2015
node = pool.at(0) is executed even though pool is empty,
I even tried pool.size() == 0, that line is still executed. What is the reason, I wonder?
Code:
NodePoolNode* NodePool::acquireNode(int x, int y, long t) {
std::stringstream key;
key << x << ":" << y << ":" << t;
NodePoolNode* node = usedNodes[key.str().c_str()];
if (node == NULL) {
[Code] ....
View 3 Replies
ADVERTISEMENT
Jun 20, 2013
i use dev c++...i write this code to reverse an array and save the result in the same one
if n=3 i expect
a[0]=0 a[1]=1 a[2]=2 (before rev is OK but after calling rev)
a[0]=2 a[1]=1 a[2]=0 (expected result )
but i get
n=3
[code].....
View 4 Replies
View Related
Aug 23, 2013
I found this piece code on the following site:
[URL]
I predicted the outcome as being 01230 as I thought the prefix decrement operator on iterator ce would prevent the final element of the list from being transformed.
I was wrong, the correct output is 01234.
So, I removed the decrement prefix and ran the test again, expecting a different result. It wasn't! The result was still 01234.
Only when I decremented ce twice did I get the result I initially expected, 01230.
why the first decrement of ce appears to have no effect?
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <cstdio>
int main() {
typedef std::list<int> L;
[code]....
View 2 Replies
View Related
Feb 11, 2014
Here is what I'm trying to accomplish (it is a rather simple program): A classroom of students are to grade a certain number of other exams. The exams should be distributed equally and RANDOMLY, every student should receive the same number of exams, and no student should receive their own exam to grade. The only problem I have is to generate unique random exams for each student. Right now, I have it set to where each exam is distributed the same number of times, every student gets the same number of exams to grade, and no one gets there own. However, I don't have any parameters that prevent one student from getting the same exam multiple time.
Here is an example output:
Student 1 will grade: 4 3 2 5 <- CORRECT OUTPUT (no exam appears more than once)
Student 2 will grade: 5 5 5 1 <- exam 5 appears three times
Student 3 will grade: 4 2 2 2 <- exam 2 appears three times
Student 4 will grade: 3 3 1 1 <- exams 3 and 1 each appear twice
Student 5 will grade: 1 3 4 4 <- exam 4 appears twice
(each exam appears four times and every student is assigned four exams. no one gets their own)
Here is my code (area of problem is close to the bottom):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void create_class (int);
int main (void) {
srand(time(NULL));
[Code] ....
I tried keeping the exams for each student in the array exam and then checking each one every time I generate a number, but that didn't work.
View 8 Replies
View Related
Apr 4, 2014
Code:
//Class header
CString m_cstrArry[5];
Code:
//Class source
void Ctry4Dlg::OnInitDialog() {
m_cstrArry[0] = _T("TEXT 0 |");
m_cstrArry[1] = _T("TEXT 1 |");
m_cstrArry[2] = _T("TEXT 2 |");
[Code] .....
View 8 Replies
View Related
Oct 12, 2013
I am trying to compare 2 strings of characters The users input containing 5 chars is compared to a table If the input is already be existent in the table the index of those chars in the table is printed Quest: how to copy the result of a printf() into an array ? The last printf() gives a sequence of numbers and I am trying to save that sequence to another array for further operation ! I have not been able to do that so far even with tmp[]=i ;
Code:
#include <stdio.h>
#include <string.h>
#define N 30
#define n 100
int main (void)
[code]....
View 2 Replies
View Related
Feb 20, 2014
I do not have code - I am just wondering if I have a method which gets input from the keyboard and returns it, how would I store that information in a new method after calling it and put the result of it into an array.
View 3 Replies
View Related
Feb 17, 2014
I am trying to print the array of string using this code but the compiler is printing null in the output:
The code is as follows:
char *arr[10];
int i;
for(i=0;i<10;i++)
scanf("%s",*(arr+i));
for(i=0;i<10;i++)
printf("%s
",*(arr+i));
View 3 Replies
View Related
Apr 7, 2013
Is it possible to prompt information from user then display the result in a one dimensional array form? If yes, how should i link them together?
View 7 Replies
View Related
Jan 21, 2015
I am currently trying to understand why this example for using an array as an argument in a function has a different result than what the lecture notes say it should be.
So supposedly sum should return with the value 28, but I get 27 no matter what. I also am not very good at reading and understanding what exactly the order of operations for this function are.
Code:
#include <iostream>
using namespace std;
int sum(const int array[], const int length) {
long sum = 0;
for (int i = 0; i < length; sum += array[++i]);
return sum;
[Code] ....
View 3 Replies
View Related
Sep 14, 2013
I tried running the code below and i got an unexpected output
Code:
#include<stdio.h>
void main() {
char a='A';
while(a)
[Code] ....
The code is supossed to give an infinite loop but instead it terminates with a=0...I tried running it with some casting like this
Code:
#include<stdio.h>
void main(){
char a='A';
while((int)a)
[Code] ....
But the output was the same as before.why the code has this unexpected behaviour???
View 7 Replies
View Related
Jun 24, 2013
I am working through Binary IO with objects and am running into a curious error.
The program executes perfectly until it hits the very end. At "return 0", the program fails. The debugger picks up SIGABRT when executing "return 0". This seems to me to indicate a deconstructor problem of some kind. However, I can't seem to find any deconstructor problems (I am fairly new at programming though).
If I comment out the following two lines:
"binaryio.read(reinterpret_cast<char *>(&studentNew1), sizeof(Student));"
"binaryio.read(reinterpret_cast<char *>(&studentNew2), sizeof(Student));"
then the program finishes exiting without error . . . . but the whole point is to be able to read from the binary file. With those two lines in the code, the program successfully reads from the file and outputs the objects to the console ,but fails at "return 0";
Here is the code:
#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;
void displayStudent(Student student) {
cout << student.getFirstName() << " ";
[Code] .....
View 5 Replies
View Related
Feb 11, 2014
I have the following piece of code which should write the contents of my vetor to y file but I am getting a very weird output in my file.
for (int i=0;i<amount;i++) {
fprintf(pFileO,"Case #%d: ",i+1);
for (int j=0;j<words[i].size();j++) {
[Code].....
As you can see this doesn't make sene because the file should also contain the exact same things as the cmd outputs. What's going on here ?
View 1 Replies
View Related
Aug 7, 2013
I'm getting unexpected output in 2 different cases. The 1st deals with bitfields. The C++ standard has this line about integral promotions:
An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the
bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.
If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.
This sounds like the value of a bitfield will always be treated as a signed int if the signed representation of the value will fit in the bits. This seems to hold true for my C compiler, but not my C++ compiler.
I tried storing a small negative value in a bitfield that has enough bits to store the sign bit and the value. But when I print out the bitfield, I always get a large number
In the example code below, I expect the output:
Code:
foo.x = -1
foo.y = -2
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = 4294967295 But I get: Code: foo.x = 31
foo.y = 6
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = -1 -------------------
The other issue I'm having is sort of similar. I'm trying to store 4294967295 into a float, but when I print it out, I get 4294967296. i've tried storing a few other large values like this and what's printed out is rarely the value I stored. I thought it might be because of some int to float conversion, so I tried 4294967295.0. Still no luck. Then I remember that defaults to a double so maybe that's the issue so I tried 4294967295.0f. Still no luck. Why can't I store the correct value here? I don't think it's an IEE format thing since I can use these values as floats on a calculator program.
The example code showing both issues is below.
Code:
#include <stdio.h>
typedef struct {
signed char x : 5;
signed char y : 3;
}my_struct_t;
[Code] .....
View 11 Replies
View Related
Jul 30, 2013
My compiler (GCC) keeps expecting an expression where it shouldn't in 1 specific piece of my code:
int zxcNewWindow( HWND parent, TCHAR *text, zxWINDOW *kid,
UINT style, int x, int y, int w, int h, int type )
// right here
{
*kid = zxDefWINDOW;
The project contains only 2 files right now and the settings are just the default for an empty Code::Blocks 12.11 project. Both files are in UTF-8 format (tried in ASCII too), I just cannot see why this is not compiling correctly. I'll post the files in the next two posts.
Edit: For those of you who didn't get what the error was from the above here's the full log:
mingw32-gcc.exe -Wall -g -DzxDEBUG -c C:MePrjscppzxGUImain.c -o objmain.o
C:MePrjscppzxGUImain.c: In function 'zxcNewWindow':
C:MePrjscppzxGUImain.c:39:10: error: expected expression before '{' token
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)
View 8 Replies
View Related
May 2, 2013
I'm just starting c++, doing tutorials. So many tutorials. I've noticed that the tutorials all assume the same thing: That the user will always do exactly as he/she is told when asked for input.
Example: "Please enter your age:"
Now, the example code might be expecting the user to type some numbers, but what if the user feels like typing out the letters of their age?
"I am ninety five thank you very much, sonny"
I could specify to the user that I only want the information in number form, but what if the user is just being a dick?
What if the user types, "none o' yer business."?
So... how to approach "fool-proofing" player input?
View 2 Replies
View Related
Oct 28, 2014
I have written this regex to match a floating point literal:
(^[[:space:]]*)(([0-9]+.?[0-9]*([eE][+-]?[0-9]+)?)|"
"(.[0-9]+([eE][+-]?[0-9]+)?))([fFdD]?[[:space:]]*)$
and when I match it with string like "123e" or "e2" it works while it shouldn't and I can't find the reason why.
View 2 Replies
View Related
Oct 9, 2013
I keep getting a segmentation error when ever I have the following code...
int main(void) {
//Section 1
unsigned long val = 12;
std::vector<unsigned long> vval;
for(unsigned long i = 0; i < 100; ++i) {
vval.push_back((unsigned long)0);
[Code] ....
Error: *** Error in `/home/alex/projects/bignum/build/bignum': free(): invalid pointer: 0x00007ffff75b5b88 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7274a46]
compiler is clang++ 3.2
It doesn't happen if I restructure it so that bignum::num is not a pointer to an std::vector<unsigned long>
View 10 Replies
View Related
Apr 1, 2014
I have a WordRecord that contains a LinkedList (both my doing). I have rigorously tested my LinkedList class, and know that it works (heck, I used it in the last project I had!). The problem is that undefined behaviour seems to happen when using the WordRecord, which has a std::string and a LinkedList<unsigned>. (The problem happens with the LinkedList.)
Here is the code:
main.cpp
#include <iostream>
#include "BinaryTreeNode.h" // here for test purposes
#include "LexicographicTree.h"
#include "LinkedList.h"//also for test purposes
#include "OutputStream.h"
#include "WordRecord.h"
using namespace std;
int main()
{
// setup the OutputStream to print to "test.txt"
OutputStream stream("test.txt");
// create a sampleWordRecord (make it have the word "I" on line 1)
[code]....
One of the requirements for the project is that it must compile on Unix server (I am using Windows, and have tested it in both environments.) I get a core-dump in the Unix environment. On the other hand, the output on-screen in the Windows environment looks right. However, when I open up the text file, I get the following
Sample word record:
WordLines
I{14}
/* I have no what is happening to sampleWordRecord's LinkedList; I am not trying to modify it, except for where I created the sampleWordRecord! */
View 14 Replies
View Related
Nov 8, 2013
So the latest challenge from jumping into c++ is as following.
Code:
Write a function that builds the multiplication table of arbitrary dimensions This chapter also talks a ton about 2d arrays.
So I've built my program thus far as this.
Code:
#include <iostream>
using namespace std;
int drawTable(int,int);
int main()
[Code] .....
So basically, the idea is that I can use the arrays dimensions as a placeholder, and just multiple them to get that specific spot, so table[0][0] = 0, [0][1] = 0 and so on. However the output actually seems to be randomly generated numbers so I'd like to ask, what am I doing wrong? Am I on the right track? Or have I missed the bus stop completely.
View 12 Replies
View Related
Apr 30, 2014
I wrote a sequential matrix multiplication program in c.
After execution i get error like
./mul.c: line 11: syntax error near unexpected token `('
./mul.c: line 11: `int main(){'
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define wA (30 * 16)
#define wB (50 * 16)
#define hA (80 * 16)
#define hB wA
#define wC wB
[Code] ....
View 4 Replies
View Related
Nov 11, 2014
My Windows service may download a binary update in the background using WinHTTP APIs. I'm using code similar to the one from the bottom of this MSDN page. For ease of access I'll copy if below:
Code:
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
[Code] ....
The issue I'm experiencing is that while downloading, the code above "hogs" most of the bandwidth available on the client computer. My goal is to make it "throttle" itself on a configurable "bps" level, say at 50 kbps and not use any more bandwidth.
Someone suggested measuring my current download speed (in bps) and depending on the value make the thread "sleep" before calling WinHttpReadData again.
So the formula I'd use to calculate BPS is this:
BPS = (BytesDownloaded * 1000 * 8) / TimePassedInMilliseconds;
Then to convert to Mbps:
Mbps = BPS / (1024 * 1024);
And the waiting/sleep would be done as such:
Code:
DWORD DesiredBitsPerSec = 50 * 1000;//for 50 kbps
if(fBps > DesiredBitsPerSec) {
DWORD dwW = (DWORD)((fBps - DesiredBitsPerSec) * 1000) / DesiredBitsPerSec;
if(dwW > 0)
::Sleep(dwW);//Sleep to throttle the download speed to the one we need
}
So I implemented it into the code above but it didn't work -- the Sleep API would produce very strange delays. So I decided to add a little bit more tracking code to actually be able to see what BPS values I'm getting.
I created a small test project that is attached to this post to illustrate the issue. (It's hard to explain without running an example.)
For a test I began downloading a large binary file (Windows 10 MSI, off Microsoft's server) and at the same time had the Task Manager open on a Performance tab that was showing me the current network throughput. I made a screenshot:
But as you can see, the reading on the test program and the actual network BPS values differ greatly.
View 1 Replies
View Related
Sep 19, 2013
I usually check if a bit is set using:
Code: bit = (number >> n) & 1; where `n` is the bit I want to check...
But I came across a stackoverflow answer saying:
bit = number & (1 << x); will not put the value of bit x into bit unless bit has type _Bool (<stdbool.h>).
Otherwise, bit = !!(number & (1 << x)); will..
So why is this? why the double !?
View 5 Replies
View Related
Nov 4, 2013
Everything seems to be correct from my perspective. heres the program: Code: /*c program to check whether a string is palindrome or not*/
#include<stdio.h>
#include<string.h>
int main(void) {
char str[30];
int i,j,flag=0;
[Code] .....
View 1 Replies
View Related
Dec 8, 2013
The reason being is that it says that my program is right
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define NFlights 10
struct date {
int month;
int day;
int year;
int hour;
int minute;
[Code] ....
View 6 Replies
View Related
Jun 15, 2013
Q. In context of C language syntax checking, which of the following can be modeled using Finite Automata?
(A) Detecting proper termination of an instruction.
(B) Detecting balance of parentheses.
(C) Detecting initialization of a variable.
(D) None of the above.
View 4 Replies
View Related