C++ :: Sending Constants As Parameters
Nov 21, 2013
What's the problem with the following:
Code:
#define K 3;
int max(int a, int b) {
return a>b? a : b;
} int main() {
cout<<max(K, K+3);
return 0;
}
Why is it not allowed, and how is it different from:
Code:
int max(int a, int b) {
return a>b? a : b;
} int main() {
cout<<max(3, 3+3);
return 0;
}
View 3 Replies
ADVERTISEMENT
Nov 18, 2013
I am working on a code that is suppose to get vowels and consnants from a string. So far i got up to trying to get the vowels from a string. this is what i have so far:
#include <iostream>
#include <string> // includes go into header
using namespace std;
int getword(string word);
int getvowels(string word);
[Code] .....
View 2 Replies
View Related
Apr 3, 2015
I would like to know how can i set a constant attribute in the constructor. This attribute is an int value that cannot be changed.
For instance:
class Test {
public:
const int x;
public:
Test(const int val);
[code].....
With this code i get compile error!
View 5 Replies
View Related
Nov 19, 2014
I have a templated class that can either use float or double as type.
My question is now: What do I do with constant numbers in the code?
Let's assume I have a multiplication by 0.5:
In the case of float type, I want: 0.5f
In the case of double type, I want: 0.5
One answer would be to check for every constant the type and doing an if/else, but this is very annoying with lots of constants.
The code using these constants infer their type automatically by the assignment, that's why I have to take care of the constants.
View 1 Replies
View Related
Jun 29, 2014
"A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.
A literal constant is a value typed directly into your program wherever it is needed. For example, consider the following statement:
long width = 5
This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can't assign a value to 5, and its value can't be changed.
The values true and false, which are stored in bool variables, also are literal constants.
A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. Here's a statement that sets the point reward for killing a zombie:
const int KILL_BONUS = 5000;
Whenever a zombie is dispatched, the player's score is increased by the reward:
playerScore = playerScore + KILL_BONUS;
If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it would be more difficult to find all the places it is used and change the value. This reduces the potential for error."
what's the difference? Here is a program to demonstrate what I'm having trouble conceptualizing.
#include <iostream>
using namespace std;
int main() {
int width = 10, length = 10;
int area = width * length;
cout << "Width: " << width << endl;
cout << "Length: " << length << endl;
cout << "Area: " << area << endl;
return 0;
}
Now, why would it be harder to go in and changed a regularly defined integer than one defined with the 'const' keyword proceeding it? For example, the width and length variables. My confusion comes from the point that they seem to both simply be variables with a value assigned to them. I feel as if the process of having to change a literal constant's value is synonymous to the process of having to change a symbolic constant's.
View 3 Replies
View Related
Mar 6, 2015
was just curios to know if there is a way to make the preprocessor compare against constants defined like this: Code: uint uint_max = -1;
View 1 Replies
View Related
Oct 13, 2014
I need to write a program in which you do the following:
Define three named constants using the appropriate data types:
DEC_NUM = 65;
HEX_NUM = 0x7a;
LETTER = 'f';
Then display each of these constants in decimal, in hexadecimal, and as a character using cout. Your program will have a total of nine cout statements.
View 1 Replies
View Related
Feb 17, 2014
My assignment is to create a simple stock broker program that ask the user how much they are willing to invest and ask what company they would like to invest in. Finally it outputs how many shares the user will have based on their investment amount. My code is below. My professor said to declare symbolic constants and factor out the if else statements. Ive been struggling trying to understand constant variables. How do I use const variables to factor out the if else statements?
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//Declare Variables
const double BAC = 16.7;
const double Citigroup = 49.52;
[code]....
View 4 Replies
View Related
Sep 28, 2013
why strcmp() doesn't return true when comparing a string constant with a string that was acquired via a linked list. By the way, the data in the linked list was taken from a text file. Does that imply that there's a new line () character in the string from the linked list?
Code:
struct Node{
char ACNO[15];
struct Node *next;
[Code]....
View 1 Replies
View Related
May 23, 2014
I'm playing around and wrote a tiny program.
Code:
char bracketin[] = "thisgetsbracketed.txt";
char bracketout[] = "bracketed.txt";
char testwalk[10] = "12345678";
[Code]....
I'm incrementing the pointer to buffer 150 bytes beyond its reserved 50. I see testwalk, followed by bracketout, followed by bracketin printed by the overflow on buffer.
The memory locations are ordered descending from their call order. Why is this the case?
One would think that they would be written in ascending order as I call them. I can only assume that they're compiled bottom up - where could I read about this process?
View 3 Replies
View Related
Sep 16, 2014
why this code works only for file of smaller data. When I try to send for istance a image, the image arrive corrupted.
Client:
//read all file
byte[] vett = File.ReadAllBytes(s);
//read filename
[Code]....
View 11 Replies
View Related
Oct 30, 2013
I have a socket connection already set up, my thing is what would be the best way to send a packet through sockets? My teacher wants us to convert the packet to bits first before we send it. I think he wants something like this:
Code:
struct packet{
int header
int message
int flag
}clientpacket; m
My question is how do we covert this to bits, before sending it. One of my friends said we can create a char[] array the size of the struct as a buffer. will memcpy() change the struct to bits if I copy it to the buffer?
this is how is suppose to look in bits for the header field/column.
Field : header
Field size(bits): 8
data format: Unsigned int
value example: 1
Value in bits: 0000 0001
I am confused, I think I send a structure over the socket but I did not convert it to bits before I sent it .
View 13 Replies
View Related
May 18, 2014
I want to apply program to send matrix from client and server ,and return back a processed matrix ..
How I can do it using UDP ,it was easy to use tcp for that!?
View 3 Replies
View Related
Dec 18, 2014
Verify my Method . I am not getting expected output ..
Code:
int Amount=500;
send(newSocket,Amount,4,0);
close(welcomeSocket);
recv(clientSocket,Amount,4,0);
// Amount= ntohl(Amount);
printf("Data received: %d",atoi(Amount[0]));
View 5 Replies
View Related
Nov 1, 2013
I want to write a program(s) so that the client sits and waits for a scanf to receive an int, then send to server, server then receives the int, processes it, then goes back to sit and wait for new input. to start with, where do i start! server side, or client side? and also what order do i have to go in my program before the loop to set up the socket, and listen? and then what gets looped to keep the connection open and wait for new receive?
View 4 Replies
View Related
Jul 21, 2013
I have a project, to make a program that spams chat programs. I've been trying to figure out how to send a string to an open program such as notepad, or a chat window. I know how to simulate keystrokes but I have yet to figure out or find out any way of sending a string to a program. pseudo code:
int main() {
string a;
int howManyTimes;
cin >> a;
cin >> howManyTimes;
//user enters "pizza"
for(int i = 0; i < howManyTimes; i++) {
//now I want "pizza" to be sent to the program keystroke enter or whatever the correct syntax is sleep
} }
View 1 Replies
View Related
Mar 1, 2013
I am writing some code to send text to a third part software. Basically each sending looks like this:
p << "set terminal eps
";
p << "set output '07.eps'
";
p << "plot '-' using ($1 == 0 ? NaN : $1) with lines linecolor 2 title 'comparison ratio',
'-' using ($1 == 0 ? NaN : $1) with lines title 'comparison ratio'
";
p.send(TOex_ar).send(TOnew_ar);
p << "set terminal wxt 7
";
p << "plot '-' using ($1 == 0 ? NaN : $1) with lines linecolor 2 title 'comparison ratio',
'-' using ($1 == 0 ? NaN : $1) with lines title 'comparison ratio'
";
p.send(TOex_ar).send(TOnew_ar);
Each time I have to change the number "07" and "7";decide to add or not the following parts: "using ($1 == 0 ? NaN : $1)", "with lines", "linecolor 2";and write the title 'comparison ratio' and the name of data to send "TOex_ar" and "TOnew_ar".Since I have to do this kind of thing 50 times in my code, and it is in the form of text, I am wondering whether in C++ we can write a template or function to simplify the program, just to input the changing parts.
View 3 Replies
View Related
Apr 20, 2014
I wanted to make an program which is sending messages to log in and password, but instead of message it gives me "System.Windows.Forms.TextBox, Text: AND HERE MY TEXT I WANTED.
View 9 Replies
View Related
Jan 10, 2015
create BOT that adds friends on one of Chat websites.
Im using C#
On website there is a button that adds user to friends
<li class="friend"><a href="javascript:void(0)">Add Friend</a></li>
I captured packet that is sended to server then that button is clicked
POST (info hided)/amfgateway.php HTTP/1.1Host: (info hided)
Connection: keep-alive
Content-Length: 52
[Code].....
It apears that this is AMF(Action Message Format) type packet Content-Type: application/x-amf it is in binary and this complicates things a little because I cannot send it with regular httpwebreqeuest class(Or I think so)
I used Fiddler4 with AMF plugin to check what data was passed with that packet
It apears that these numbers under content / 0 are User-id that is needed to add user to friends
I did a research with little success finding any usefull examples. I just found that there is a library for FLEX/FLASH remoting called fluorinefx and this should do the job but when I opened documentation it seems little too complex for me.
View 2 Replies
View Related
Jul 23, 2014
If I have a text file named, stuff at a specified path in the c : drive, how do I send the contents of this file to another computer using the Internet?
View 5 Replies
View Related
Nov 4, 2013
I am trying to get all these functions work together and send the value of countX and countY back into main from function2() and function3() to be used by function4() later on.. But I keep getting 0 printed out and I am not quite sure why.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define maxrow 20 //defines maxrow as a constant of 20
#define maxcol 30 //defines maxcol as a constant of 30
}
[code]....
View 2 Replies
View Related
May 11, 2013
I want to send data from a file via winsock. The problem is that the only first line in the file is send.
Client
#include <iostream>
#include <winsock2.h>
#include <fstream>
#include <string>
#pragma comment(lib,"ws2_32.lib")
#define SERVER "127.0.0.1" //ip address of udp server
[Code] ....
View 2 Replies
View Related
Dec 17, 2013
I am trying to convert decimal value 1 to 32 to hex value and insert it at 4th pos of char array and send it as hex value at the serial port .
My issue is it is converting to decimal to hex value but when it sends, it treat the converted hex value as char and sends it equivalent hex value.
For example
if decimal =1 then its hex = 1.
so , writebuffer[3] =1
But when we send whole writebuffer through send function, it treat this 1 as char and sends its hex value as 31. how to send its hex value.
unsigned int i=0;
char hex[3];
unsigned char hexunsigned[3];
unsigned int dec;
do {
unsigned char writebuffer[8] ={0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
[Code] ....
The send function is
void serial::send(unsigned char data[], DWORD noOfByte) {
DWORD dwBytesWrite;
WriteFile(serialHandle, data, noOfByte, &dwBytesWrite, NULL);
}
View 2 Replies
View Related
Feb 3, 2014
I've got something I'm trying to accomplish, but crashes my program.
I've got my server and client code.
Having the client send a message they type (char Chat[1024]) And the server receiving the chat (char recv_chat[1024]) only to send it to all connected clients again. Which the server sends the recv_chat.
The client receives it (char recv_chat[1024]). This works, and the client gets the right info. However, I'm trying to store it using a vector. I'm sure I've tried any way possible.
Client storing vector pseudo-code:
vector<char*> SaveChat;
int main () {
while (true) {
if (newClientConnected) {
[Code]....
This doesn't work, and crashes my application. I've tried changing the vector to string, const char*, basically anything I can with no avail.
View 8 Replies
View Related
Mar 21, 2015
I'm building a project in my free time and in the last part of it I need to fill a form of a website and then 'hit submit', but how it can be done. Btw the form is a 'POST' form so it's harder than a 'GET' one.
View 2 Replies
View Related
Apr 21, 2014
How to send a keystroke or a mouse event to another running process in Ubuntu? I want to write a C program that reads data from the USB port sent by AVR Micro Controller board continuously.In response to that it checks the data received and sends a command to another process running on the computer? Example,when the program reads 101 from the USB port it sends left mouse button down to the VLC media player window that is currently running?
View 1 Replies
View Related