C :: Declaring Array In If-Else Construct

Apr 4, 2013

Code:

if (IS_LEAP_YEAR(year))
const int days_per_month[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
const int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Is it ok to declare the array in this manner or is it bad?

And i have to ask the user for a date to enter in my program. So should I use scanf or should I store the date in a string and then use sscanf. I have to check for valid input for everything like day, month, year etc. I did it as below..

Code:

int assignments;
assignments = scanf("%d / %d / %d", &month, &day, &year);
fflush(stdin);
if (assignments != 3)
{
printf("Retry: ");
}
else
error checking.

View 9 Replies


ADVERTISEMENT

C++ :: Declaring Array To Be Empty

Jun 30, 2014

We have been assigned to create an iTunes library. Everything compiles in my other .h file but my main is not happy with my object declaration. It keeps stating "primary expression before '{'". Here is my main code:

#include<iostream>
#include<string>
#include<fstream>
#include"myTunes.h"
using namespace std;
//function protocols
void read(string);

[Code] ......

View 1 Replies View Related

C++ :: Declaring Array To Be String?

Mar 18, 2013

I'm writing this program that basically interprets the rottentomatoes website. I am however having a problem declaring if it is rotten or fresh according to the rating the user enters.

I'm outputting it here:

void PrintAll(const string titles[], const int ratings[], int count) {
WriteLine('=', 50);
cout << "PRINT ALL" << endl;
WriteLine('-', 50);

[Code] .....

And here is my condition:

string RatingToString(const int ratings[], int count) {
string rank;
for(int i = 0; i < count; i++) {

[Code]....

Here is the output:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
==================================================
MENU
1. Add Movie
2. Print All
3. Exit
--------------------------------------------------
Enter 1-3 : 1
Title : Hitch
Rating : 90
==================================================

[Code]....

My condition works when there is only one movie, but when I add more, it gives it the new movies "ROTTEN" or "FRESH" rank.

View 1 Replies View Related

C/C++ :: Declaring Array Using Sizeof?

Oct 29, 2013

I have the following code:

struct mystruct {
   char fieldA[5];
   char fieldB[7];
};  
void dosomething(struct mystruct* pms) {

[Code] ....

Is there any problem doing that, even for a C89/90 compiler?

View 3 Replies View Related

C++ :: Declaring Array At Runtime?

Oct 21, 2012

I can not set the size of my array while running porgrama. Is there any way to do this in C + +?

---- code ------

#include <iostream>
#include <string>
using namespace std;

[Code].....

View 5 Replies View Related

C :: Declaring Array Within Function Argument

Sep 14, 2013

I have a function

Code:

int exec_program(char * arguments[])
{
...
}

I can call it like this without a problem:

Code: char * uselessvariable[] = {"/bin/echo", "Testing", NULL};exec_program(uselessvariable);

However I get an error if I try to compile it like this:

Code: exec_program({"/bin/echo", "Testing", NULL});

How, in c, I can put this array inside of the argument in one line without having to name a new variable name?

View 2 Replies View Related

C++ :: User Declaring The Size Of Array?

Dec 1, 2014

is it possible to use this code ?

#include <iostream>
int main(int argc, const char * argv[]) {
int number = 0;
std::cout << "napis cifro za array
";
std::cin >> number;

[Code]...

It does work on xcode but doesn't in microsoft visual studio 2013. Where is the problem?

View 3 Replies View Related

C++ :: Declaring Array Inside A Class

Apr 18, 2013

class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------

int numLights;
int lights[MAX_LIGHTS];

[Code] .....

I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.

How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.

View 6 Replies View Related

C :: Declaring And Initializing 3000 Number Array?

Oct 23, 2013

Here are the steps to this program:

1. Declare an array that will hold 3000 numbers
2. Initialize this array by assigning a random number to each element in the array
3. Traverse the array, modifying the current contents of each element in the array so that each value now lies between -3000 and 3000 inclusive
4. Traverse the array to compute the average value of all elements

I have never worked with arrays before and am lost!

View 2 Replies View Related

C :: Declaring Array Size Using Variable Causes Program To Crash

May 10, 2013

I am new to C. I've been trying to use C to code some statistical functions originally coded in R. I've encountered an interesting phenomenon. In the function foo1, I declared the array v1v2b using an actual value 1999000. The function runs fine when I call it in R.

Code:
void foo1(double *x, double *y, int *nsamp){
int i, j, k, oper=2, l;
double* v1v2=malloc(sizeof(double)*((*nsamp)*(*nsamp-1)/2 + 1));
outer_pos(x, y, nsamp, &v1v2[0]);
double v1v2b[1999000]; //<-------HERE
for(i=1; i<= 1999000]; i++){
v1v2b[i-1]=1;
} }

However, in foo2, I first create an integer variable called index, and store the value 1999000 in it. I then use it to initialize the same array. When I tried calling this function in R, it either led to a stack overflow error, or completely crashed R.

Code:
void foo2(double *x, double *y, int *nsamp){
int i, j, k, oper=2, l;
double* v1v2=malloc(sizeof(double)*((*nsamp)*(*nsamp-1)/2 + 1));

[Code] .....

View 9 Replies View Related

C :: Declaring Pointer Variable To Array Of Function Pointers?

May 24, 2013

I am having following pointer variable declaration

Code: Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};

Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.

View 2 Replies View Related

C++ :: Flash Card Type Console Application - Declaring Array Of Hexadecimals

Dec 26, 2013

I'm making a flash card type console application using visual studios 2013. The flash cards contain character that I can display using unicode. So far I am looking at about 200 characters across 2 unicode blocks which I don't want to hard code into my arrays. I thought of initializing my arrays using a loop. The only problem is I don't know how to add in hexadecimal. So is there a way to initialize my array without having to input 200 values my self? Also is hexadecimal addition possible without me having to write a function for it?

View 3 Replies View Related

C :: IF Construct And Printing Strings

Apr 26, 2014

This code i made divided a user input into characters and non character. The problem im having is that if a mixed sentence is created, such as 32B, it will only print the 'char string' and not the 'non char string'. But when the sentence is just non characters like 32 it will print the 'non-char string'. So essentially if a mixed sentences is created both of the strings won't be created or printed.(This is only a function by the way).

Code:

Count_chars( char Input[]) {
int i;
for(i = 0; Input[i]; i++) {
if((Input[i] <= 122) &&(Input[i] >= 97)){
printf("Char %c ",Input[i]);
Char[i] = Input[i];

[Code]...

View 4 Replies View Related

C++ :: Construct Maps Initialization

May 5, 2013

I've a problem with the map construct. I wrote this class in which there are maps:

class Features {
private:
map<const string,GenericFeatureContainer*> featuremap;
map<const string,GenericFeatureContainer*>::iterator it;
int size;
public:
}

And I have the following constructor:

Features::Features() {
map<NULL,NULL> featuremap;
it=NULL;
size=0;
}

Is that correct? otherwise what is the correct syntax?

View 2 Replies View Related

C++ :: Compute Mean / Median / Mode For Map Construct

May 28, 2013

I am writing code, which reads an input file (280 MB) containing a list of words. I would like to compute

1) total # of words
2) total # of unique/distinct words
3) mean/median/mode of word count (Link)

I managed to get done with 1) and 2), but my program crashes for 3). I am not quite sure whether there are memory issues or some bug in the code.

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

[Code] .....

View 2 Replies View Related

C :: Utilizing Type Casting Construct

Apr 13, 2014

This code i made, utilizing the type casting construct, isn't outputting what i wanted. The output for 'Dollars' and 'Cents' are returning '0' for both. instead all i want it to do is seperate the two. for example changing the float value of amount to an integer, giving a dollar value.

Code:

#include <stdio.h>
int main()
{
}

[code]....

View 1 Replies View Related

C++ :: Construct Simple Hash Function?

Jun 3, 2013

How to construct a simple hash function ? When the program executes, it will ask for your name. Type your name and it will print out a "hash code" (a number) from that name.

View 2 Replies View Related

C++ :: Use Construct Object (Shape) Efficiently?

Jun 27, 2014

have a problem with my code (I wish for answer with code). The conditions I have to grant:

- **Only** a pointer to an object must be saved in a **standard** class vector (e.g vector<ShapePtr>)
- Base class must be a polymorphic class (I call this class ShapePtr)
- I **must** have a deep copy process
- Constructor is **not** suppose to do a deep copy
- **clone()** is suppose to do the deep copy

Here is the code **provided** to me:

main.cpp
int main() {
typedef shared_ptr<Shape> ShapePtr;
vector<ShapePtr> shapevec;

[Code].....

View 1 Replies View Related

C/C++ :: Why Cannot Use Variables Inside A Case In Switch Construct

Feb 26, 2015

If I have an integer variable like int a=9 then in the switch case If i write :

switch(a) {
case 4+a: printf("hii");
}

Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.

View 1 Replies View Related

C :: Void Functions With If Else Construct Not Printing Multiple Lines

Apr 12, 2014

This code i made is a cent converter from 5 to 95 cents. The problem i'm receiving is when the 'cents' function is sent back to the 'main' function it only prints one line. It seems to just print the first if construct that complies with the statement. Is there anyway i can have this function print multiple cent values? For example if 60 cents was entered it would only print '50c', and i want it to print '50c' and '10c' instead.

Code:

#include <stdio.h>
int x;
void check(int x)
{
if( x < 5)
printf("Less then 5 cannot be calculated
");
else if(x > 95)

[code]....

View 3 Replies View Related

C :: Read Characters From User Input And Construct A String

Mar 13, 2013

how to read characters from user and construct a sting and then extract part of it using substring?

View 2 Replies View Related

C++ :: Construct A Program Without Main Function That Output Stars

Nov 5, 2013

I am trying to construct a program without a main function that outputs stars but at the same time outputs the number and a colon after the number but before the number of star. This is the coding i have so far.

void output_stars(int num){
if (num > 0){
cout << "*";
output_stars(num-1)

[Code] ....

The program is working the way it should i just can't figure out how to output the number and a colon. The main function is written as such:

output_stars(1);
output_stars(2);
output_stars(5);
output_stars(3);
output_stars(4);
output_stars(7);

How to get the numbers of the main function to show up with the stars of the previous function.

View 6 Replies View Related

C/C++ :: Reading From File To Construct A Pointer Based Maze?

Nov 29, 2014

I'm trying to read a "pointer-based" maze .txt. The first letter in each row corresponds to the room letter...then the letters that follow are North node, East node, South node, and West node respectively. The asterisk indicates an empty room or not a valid option.

Here is what I have come up with, what is happening is after the file is parsed by read_maze it is calling my is_empty function indicating that there is no maze because it doesn't go into the else statement here.

I've attached a sample input file:

 maze.txt (130bytes)
Number of downloads: 19

We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.

void Maze::read_maze(string FileName){

string line;
ifstream inStream;
inStream.open(FileName.c_str());
int test = inStream.peek();
int i = 0;
if (!(inStream.fail())){
while (!inStream.eof() && test != EOF){

[code]....

View 5 Replies View Related

C/C++ :: Switch Construct Does Not Allow Negative Values To Be Used Inside A Case?

Feb 26, 2015

I just wanted to ask the reason that why is the below code not checking the case -1 while working for the other case values.

#include<stdio.h>
#include<conio.h>
int main() {
int i=-1;
switch(i-2) {

[Code] ....

So why in the below code the case -1 doesnt run,when the value evaluated by the switch construct is actually a negative integer constant.

View 1 Replies View Related

C++ :: Construct Truncated Gaussian Distribution In The Range Of (5-20 Microns)?

Apr 19, 2014

I need to construct Truncated Gaussian distribution in the range of (5-20 microns), specifyed by initial standard deviation.

Particularly i need input which comprises of minimum value(5 microns), maximum value(20 microns) and standard deviation of (5,10,20 etc..). In this regard i tried with Box-Muller but it doesn't fulfuil the goal.

View 2 Replies View Related

C++ :: Read In Lines From A File / Store In Variables Then Construct Instances Of Class

Aug 22, 2013

I can't get my code to compile, i need to read in lines from a file and store them in variables. Then i have to construct instances of my class for how many lines there are in the file and take those variables into them.

I'm getting this error :

"a2.cpp:40: error: cannot convert `Employee' to `Employee*' in assignment"

#include<iostream>
#include<string>
#include<fstream>
void displayInfo();
using namespace std;
class Employee{

[Code] .....

View 1 Replies View Related







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