C/C++ :: Creating Constant At Runtime With User Entered Value?
Aug 14, 2014
At the moment I am making program that will use a 2d selection of "cells" to make a "map" of sorts. However, the size will not be known until run time.
I figured using an array would good, because once the size is chosen it won't be changed. However I can't create an array without using a constant.
So the first question is, can I create a constant at run-time with a user entered value that can then be used for an array size? If so, how?
Otherwise, what are my options to achieve this? I know vectors can be used, but A, my compiler keeps giving me problems even when C&P some code bits (yes I even remembered to #include and such) and B, I noticed that vectors reserve extra memory for when the size changes but this is bad (well my dinky little program won't notice, but trying to set good habits as I learn, so I'm keeping it in mind) as I don't need and don't want to allow the size to change after creation.
Which leads to number three, if I do have to use vectors, how can I prevent any accidental size changes after the initial size is determined?
View 4 Replies
ADVERTISEMENT
Jul 15, 2014
I am trying to find the max number entered by the user, and it should terminate when a negative number is entered. For my code, it will just end when the user inputs a lower number than the previous. i.e.- 10 20 15 "The highest number is 20" when it should be "10 20 5 40 15 -1" "The highest number is 40". No arrays or do/while loops either.
#include <iostream>
using namespace std;
int Max(int x);
int main() {
int x;
[Code] ....
View 9 Replies
View Related
May 6, 2014
i need to creat a program in la user give me "X" number and creat the textbox and y need sum all textbox
int t = Int32.Parse(txtbano.Text);
for (int i = 0; i < t; i++)
{
[Code]....
View 8 Replies
View Related
Apr 10, 2013
I receive messages over a bus. Suppose there is a function to read the messages storing them into a struct defined as follows:
typedef struct
{
ULONG timestamp;
BYTE ID;
BYTE data_length;
BYTE data[8];
} MSG_FRAME;
Depending on the message ID different messages represent different values for one project.For example msg with ID 10 can include in the 8 bytes something like:
width: 6 bits
height: 6 bits
actpos: 12 bits
maxpos: 12 bits
minpos: 12 bits
range: 16 bits
total: 64 bits = 8 bytes
Printing the message is no big deal. But here comes the tricky part. I want to print out the specific information hidden in the 8 bytes. I can define the structures for every msg ID and compile the program with this "special" header file, but I want to do it during runtime of the program, loading the information regarding the msgs, because i can have different projects where the information for different msg IDs can differ.
I've a non-C file, where basically all the information is written. Lets stay frame named
GetStatus{
bit 0 - 7 width
bit 8 - 15 height
.
.
}
etc.
How to read it on runtime and decode the messages? On runtime I'm not able to create variables and structures anymore!
View 13 Replies
View Related
Oct 17, 2014
I've been given specific instructions to create an array inside a Class Matrix using a constant n. This is my class but I am getting errors. I thought that maybe I had to initialize the const and the array using the constructor function Matrix() instead of directly in the class, but I didn't have any luck with that either.
class Matrix
{
public:
Matrix();
private:
const int n=3;
int e[n][n];
};
View 4 Replies
View Related
Apr 4, 2014
Define a constant PI that has a value of 3.14159
- Create a double variable, Radius with an initial value of 10
- Create two double variables. Circum and Area, without initialization
- Using the following formulas, compute circumference and area of the circle:
circumference = pi * r * 2 (here, r means radius)
area = pi * r * r
- Display the result using three variables (numbers must come from variables)
- Expected output: (Don’t forget to display the period at the end of the first line)
Circle with radius of 10.
Circumference = 62.8318 Area = 314.159
View 3 Replies
View Related
Mar 23, 2014
I'm doing a project. And I want to give the user the ability to write methods just like you would regularly in c# during runtime and then use them during runtime. Is such thing even possible? If so how?!
View 12 Replies
View Related
Apr 6, 2013
I want to extract expressions like <23.34 463.2 23.2 + *> in program runtime from the user. (I am trying to evaluate postfix expression)....
View 3 Replies
View Related
Sep 16, 2014
when i declare local variable x and use it in array,the error is occure that use of un assign local variable.
namespace ConsoleApplication11
{class Program
{
static void Main(string[] args)
{
int x;
Console.Write("enter how many entries you want");
[Code]...
View 2 Replies
View Related
Mar 9, 2013
I am using Dev C++ on a windows computer.
In my code, the user enters the name of a city, and then according to what city it is, the program displays the coordinates of that city
I can't find a way of figuring out and checking which city the user has entered so the code can displays its appropriate latitude and longitude.
View 9 Replies
View Related
Feb 14, 2014
So, I am making a name-database-type-thing and I want the user to be able to enter there own file. Like, here:
string fileName;
string name;
cout<<"What is your file name?"<<endl;
getline(cin, fileName);
ifstream nameFile(fileName);
getline(cin, name);
nameFile<<"Name: "<<endl;
Well, that last part is in a loop, so you can enter an unlimited number of names to the .txt file.
So, it won't let me use the string in ifstream. How can I fix this? This maybe unclear, so if it is just say so and I will edit it.
View 6 Replies
View Related
Mar 19, 2013
I am making a program that will suggest meal options when choosing specifics contained within, e.g. style, base, main, sauce.
What I would like to know is how to collect the input that has been entered by the user, say you are given 4 lists and each has an option, how could I chain these together in order to cout a suggestion that is in my database?
-------------------------------------------------------------------------------
Example (not my real Q's and A's):
OUTPUT: What meat would you like?
INPUT: "Beef" //<collect this!
OUTPUT: What sauce would you like?
INPUT: "Tomato" //<and this!
OUTPUT: What would you like to have as the main/base?
INPUT: "Pasta" //<and this!
OUTPUT: I can suggest... "Spaghetti Bolognese" //< and return this from the database using the 3 filters
--------------------------------------------------------------------------------
View 1 Replies
View Related
Oct 3, 2013
/*
* echoString2.c
* Echoes a string entered by user. Converts input to C-style string.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void) {
char aString[200];
char *stringPtr = aString;
[Code] ....
and these are my errors
212Untitled1.cpp[Error] stray '222' in program
212Untitled1.cpp[Error] stray '' in program
212Untitled1.cpp[Error] stray '222' in program
262Untitled1.cpp[Error] stray '222' in program
262Untitled1.cpp[Error] stray '' in program
262Untitled1.cpp[Error] stray '222' in program
Untitled1.cppIn function 'int main()':
2125Untitled1.cpp[Error] 'n' was not declared in this scope
View 3 Replies
View Related
Jul 28, 2014
I'm making a Project on a Travel Agency and I need to hide the password entered by the user! I am working on Xcode 5.1.1 -> Command Line Tools -> C++ on my MacBook Pro! Here is what I've tried --
string pass ="";
char ch;
cout << "Enter pass";
ch = getchar();
while(ch != 13){//character 13 is enter
[Code]...
But in the first case the display is -
//Assuming the password is Hello Hello ******
And in the second its giving me 3 errors -
1. Apple Mach-O Linker(Id) Error "_stdscr", referenced from:
2. Apple Mach-O Linker(Id) Error "_wgetch", referenced from:
3. Apple Mach-O Linker(Id) Error clang: error: linker command failed with exit code 1 (use -v to see invocation)
View 10 Replies
View Related
Oct 6, 2014
how I can ignore strings from being entered by the user. When the use enter's a string it always evaluates it as even. I though I might use a cin.ignore(); but I am unaware of how to use it.
#include <iostream>
using namespace std;
int main() {
cout << "Enter a number: ";
int num = 0;
[Code] ....
View 2 Replies
View Related
Mar 4, 2013
Code:
#include<stdio.h>
main() {
int item_code, quantity;
char ans, y;
[Code] ....
View 4 Replies
View Related
Feb 11, 2013
In my little experiment i am trying to get the compiler to recognize 1 word with 2 parts from a list of names.
For example: if the user wanted to choose "candy bar", from a list of items which include: "candy bar", "cat", "dog"
My current code can recognize words without a space like cat and dog. But how can i recognize candy bar?
I tried using getline but its of no use.
View 2 Replies
View Related
Aug 26, 2014
Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values,and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use cm.
View 9 Replies
View Related
Oct 29, 2012
In this exercise, write a rational number calculator that ask the user to enter two rational number:
a/b + c/d
and produce the result:
(ad+bc)/bd
View 10 Replies
View Related
Jan 10, 2014
How to do this?
View 10 Replies
View Related
Mar 6, 2015
How to find the largest and smallest number entered by a user. So far I'm able to add, find the average and count how many numbers are entered.
I'm just having trouble with find the max and min. I tried if statements and it breaks the program will it wont let me exit the while loop or the program will do a force close after entering a value.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int maxNum=0, minNum=0, value, count=0, sum=0;
double average;
[Code] ....
View 4 Replies
View Related
Mar 26, 2013
The program should find and delete all vowels in a word that is user entered. This is what I have so far and I know it should be essentially this format I just don't know how to set enteredword and word equal to each other.
#include <string>
#include <iostream>
using namespace std;
void vowelremover(string&);
int main () {
string word;
[Code] ....
View 3 Replies
View Related
May 7, 2013
So I have an array of my own class, "Restaurant," but I'm having a hard time figuring out how to store the info entered by the user into the array.
In total, I have two classes. I will show you all I have, it's a lot but I know it will be easier to understand.
My problem is in the AddRestaurant(), since that is where I should ask the user for all the information and store it in the array.
Also consider that I have to use the FTime class as well to handle the time. Basically I want to do something like this:
info[count].SetRating();
which will put the rating part of the class Restaurant into the array[count]
Also, my program doesn't compile because of the AddRestaurant and PrintAll functions since what I am showing is what I want to do, not what works.
//main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Restaurant.h"
#include "FTime.h"
int ReadMenuChoice();
[Code] .....
View 4 Replies
View Related
Nov 18, 2014
exercise in c: write a program which calculates % of even numbers entered by a user in an array;
1) check if user hasn't entered float value; if has issue warning;
2) give the option for user to press "stop" to exit program any time.
int c,sk[100],i,sum1=0, sum2=0, rel;
printf ("How many numbers will you enter?
");
scanf ("%d", &c);
printf ("Enter %d numbers (to exit program press - stop)
", c);
[Code] ....
View 1 Replies
View Related
Apr 6, 2014
I am trying to make a program with a Cartesian class that allows the user to enter 2 coordinates and displays these coordinates. When I try to compile it, I get a message saying the x and y in x=c and y=d don't name a type. How can I fix this? Also, how would I go about inserting an assignment function that assigns the values of coord1 to coord2?
Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian {
private:
double x;
double y;
[Code] ....
View 9 Replies
View Related
Jan 12, 2014
I would like to know the algorithm or flow for finding unequal elements in an array entered by user. I tried using for loop but didn't get the expected output
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
int a[10],b[10]; //array of integer elements
[Code] ....
View 6 Replies
View Related