I would read this sticky
gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven
Narue beat me to the punch...Here's my simple solution
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 16
#define LOW 0
#define HIGH 15
int mya[ARR_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int main(int argc, char**argv)
{
int i = 0;
int j = 0;
int count = 0;
int index = 0;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
for (; i < ARR_SIZE; ++i, ++count)
{
index = rand() % (HIGH - LOW + 1 - count) + LOW;
fprintf(stdout, "ans->%d\n", mya[index]);
for (j = index; j < (ARR_SIZE - 1); ++j)
{
mya[j] = mya[j + 1];
}
}
exit(EXIT_SUCCESS);
}
Please post properly format code like below
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<iomanip>
using namespace std;
using std::setprecision;
using std::fixed;
using std::transform;
using std::getline;
struct BankAccount
{
int accountNumber;
double balance;
double annualRate;
};
int main()
{
BankAccount oneAccount, twoAccount;
double amount;
int interestRate;
const double ONE_ACCOUNT = 0.0;
const double TWO_ACCOUNT = 100000;
int x;
double total = 0;
int years = 10;
int count = 1;
const double RATE = .12;
for(x = 0; x < .12; x++)
cout << x << endl;
{
cout << "Enter account number " ;
cin >> oneAccount.accountNumber;
cout << "Enter account balance " ;
cin >> oneAccount.balance;
cout << "Enter account two number " ;
cin >> twoAccount.accountNumber;
cout << "Enter account two balance " ;
cin >> twoAccount.balance;
}
if(oneAccount.accountNumber == twoAccount.accountNumber)
cout << endl << "Enter transfer amount " << endl << "from account one to account two ";
cin >> amount;
while(oneAccount.balance == 10)
{
cout << "Your account balance is $10.00 ";
if (oneAccount.balance <= 0)
{
cout << "Insufficient funds ";
}
if(twoAccount.balance >= TWO_ACCOUNT)
{
cout << "Your account balance will exceed the max amount of $100,000.00 ";
}
}
cout << "Enter the amount to invest ";
cin >> amount;
cout << "Enter the number of years to invest from 1 to 10 years ";
cin >> years;
do
{
cout << "The goal for year " << count << " is " << amount << endl;
amount = amount + amount * RATE; …
This line
while(oneAccount.balance = 10)
Do you really want to assign 10 to oneAccount.balance?
If I was to guest, I would investigate this line
CFLAGS = $(NORMCFLAGS)
What is NORMCFLAGS and is it compatible with g++?
Its a good idea to initialize local variables but if the variables going to be assigned to right away then you can leave them uninitialized...Myself, I always initialize local variables. I like knowing that my program is in a valid state.
One case that's essential is pointers. A pointer should be defined with a valid memory address or set to NULL.
Could you post your errors.
As you can see in the function useLocal the value of variable x is set to 25;at the first call, its value is still 25, then is incresead of 1, and when it's called another time,its value is 26. Ok, so far so good!!
Are you sure about that? In the second call, x should start with the value 25.
Now your function that contains a static variable, Static variables are well static and they have lifetimes that last as long as the program. How is this accomplished? In my implementation of C, its accomplished by declaring all static variables global but with controlled access and because they are global(on my implementation) I can write code like so...Note you shouldn't write code that breaks what the compiler, compiles.
#include<stdio.h>
int* useStaticLocal(void);
int main()
{
int *iptr = useStaticLocal();
fprintf(stdout, "ans->%d\n", *iptr);
return 0;
}
int* useStaticLocal(void)
{
static int x = 50;
printf("\nlocal static x is %d on entering useStaticLocal\n",x);
x++;
printf("\nlocal static x is %d on exiting useStaticLocal\n",x);
return &x;
}
Now you have to check the characters of each line...checking for things like if, while, do and then check the last printable character to see if it is ';'.
Line 50
write_msg == "How are you my child?"
This compares pointer values. If you want to test the c-strings for equality then use strcmp() or use a C++ string object.
Try something like below..I would go over the code because I wrote this quickly
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END 1
int main(void)
{
char write_msg[BUFFER_SIZE] = "Greetings";
char read_msg[BUFFER_SIZE];
pid_t pid;
int fd[2];
if (pipe(fd) == -1)
{
fprintf(stderr,"Pipe failed");
return 1;
}
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork failed");
return 1;
}
if (pid > 0)
{
char ch;
write(fd[WRITE_END], write_msg, strlen(write_msg) + 1);
while (read(fd[READ_END], (char*)&ch, sizeof(char)), ch != '\n')
{
fputc(ch, stdout);
}
fputc('\n', stdout);
close(fd[READ_END]);
close(fd[WRITE_END]);
}
else
{
char the_return_message[] = "this is the return message!\n";
read(fd[READ_END], read_msg, BUFFER_SIZE);
printf("child read from parent: [%s]\n",read_msg);
write(fd[WRITE_END], the_return_message, strlen(the_return_message));
close(fd[WRITE_END]);
close(fd[READ_END]);
}
return 0;
}
Your code doesn't make sense. Why are you using fgets() on floats? Try setting up like this.
#include <stdio.h>
#define ARR_SIZE 5
int main()
{
float nums[ARR_SIZE];
int i = 0;
for ( i = 0; i < ARR_SIZE; i++ )
{
printf("\nEnter value %d:", i + 1);
fscanf(stdin, "%f", &nums[i]);
}
for(i = 0; i < ARR_SIZE; i++)
{
printf("%d - %f\n",(i + 1), nums[i]);
}
return 0;
}
Somewhat. Please actually READ the link. There are still problems with your formatting. Yours, too, gerard4143...
Really? Is it this line?
for(i = 0; i < strlen(string1) - CHAR_SET; i++)
should be
for(i = 0; i < (strlen(string1) - CHAR_SET); i++)
A few format pointers, don't comment standard C functions. We know what they are and how they work or we can readily find information on them.
Here's how I would start this code
#include <stdio.h>
#include <string.h>
#define CHAR_SET 3
int main()
{
int i = 0, j = 0;
char string1[40], temp[CHAR_SET + 1];
temp[CHAR_SET] = '\0';
printf("Enter in a string: ");
fgets(string1, 40, stdin);
for(i = 0; i < strlen(string1) - CHAR_SET; i++)
{
for(j = 0; j < CHAR_SET; j++)
{
temp[j] = string1[i + j];
}
fprintf(stdout, "char set->%s\n", temp);
/*now compare the set with the remainder of the string*/
}
return 0;
}
Never use gets, use fgets instead. From my help file
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
Could you explain what the second function does.
Try reading your input a character at a time and checking that each one is a digit or a valid numeric symbol.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MULTI 10
int main(int argc, char**argv)
{
char ch[2];
int x = 0;
ch[1] = '\0';
while ((ch[0] = fgetc(stdin)) != '\n')
{
if ( isdigit(ch[0]) )
{
x = (x * MULTI) + atoi(ch);
}
else
{
/*handle none didgits*/
}
}
fprintf(stdout, "x->%d\n", x);
return 0;
}
When you pass a character array to scanf, you don't have to use the address of operator because the array name is the address of the array..Also the main function should be defined returning an int and i is used uninitialized on line line 41.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Date {
short Jour;
short Mois;
short Annee;
};
struct Ouvriers {
char Genre;
long NumRegistre;
char Nom[25];
char Prenom[30];
char Rue[50];
int CodePostal;
char Ville[20];
char Specialite[20];
struct Date DateNaiss;
struct Date DateEngag;
};
void AfficheOuvri(struct Ouvriers[], int);
int EncodeOuvri(struct Ouvriers[], int);
int main ()
{
struct Ouvriers Personne[100];
int nOuvrier = 0;
int nFemme;
int nHomme;
int Out;
int Ret;
int i;
long NumReg[100];
int Index[100];
char Data[100][56];
do
{
nOuvrier = 0;
Out = 0;
Ret = EncodeOuvri(Personne, i);
if(Ret == 0)
{
Out = 1;
}
nOuvrier++;
}while(Out != 1);
for(i = 0; i < nOuvrier; i++)
{
AfficheOuvri(Personne, i);
}
return 0;
}
int EncodeOuvri(struct Ouvriers Personne[], int i)
{
int Etat = 0;
printf("\n\nEncoder le genre: ");
fflush(stdin);
Personne->Genre=getchar();
if(Personne->Genre != '\n')
{
printf("\nEncoder le numero de registre: ");
fflush(stdin);
scanf("%ld", &Personne->NumRegistre);
printf("\nEntrez votre nom: ");
fflush(stdin);
scanf("%s", Personne->Nom);
printf("\nEntrez votre prenom: ");
fflush(stdin);
scanf("%s", Personne->Prenom);
printf("\nNom de rue: ");
fflush(stdin);
scanf("%s", Personne->Rue);
printf("\nCode postal: ");
fflush(stdin);
scanf("%d", &Personne->CodePostal);
printf("\nVille: ");
fflush(stdin);
scanf("%s", Personne->Ville);
printf("\nSpecialite: ");
printf("\n\t1. Coffreur");
printf("\n\t2. Ferrailleur");
printf("\n\t3. Couvreur");
printf("\n\t4. Menuisier");
printf("\n\t5. Macon");
printf("\n\t6. Manoeuvre");
printf("\n\t7. Grutier\n");
printf("Votre choix: ");
fflush(stdin);
scanf("%s", Personne->Specialite);
printf("\n\n\n");
Etat = 1;
} …
Cause your program asks for the trailing ",".
I aint ganna lie. I have no clue how to attempt this I have read about it online but every time i attempt to i fail miserably.
I would delete the compiler/IDE and then reinstall it.
You don't include header files in the compile line...It should be
g++ Agent.cpp -o Agent
I would use an if statement
if (some condition)
{
}
else if (some other condition)
{
}
else if (more conditions)
{
}
else
{
}
You can't us variables with case...It must be a integral constant.
switch ( expression )
case constant-expression : statement
[default : statement]
Here's one, passing arguments to a program with argv.
int main(int argc, char**argv)
{}
Not to steal some of the spotlight from the thread, but I'm confused by what you mean when you say this.
Most languages have facilities that allow you to extend the language..These features are usually implemented in C. Python is a good example of this.
In the wise words of - Lippman/Koenig and Moo don't assume anything. Write your header files uncomplicated...don't assume an environment that may not exist.
What do you mean by that?
Header files shouldn't assume anything. They should be simple and plain...without any preconceptions...we can't assume that the programmer will state 'using namespace std'.
Please read Accelerated C++ or Ruminations on C++ or C++ Primer...All these books state, that header files should be unqualified.
Okay, to use
vector<int> v1;
without the need of the preceding std::, you need to include the
using namespace std;
at the beginning of the header file, else std:: will be required.
Well its O.K. if its not in a header file where we can assume things like the user will use things like 'using namespace std;'
Maybe you should consider that most computer languages use C as its interface to the outside world. Why would a language so foreign to C use C as its interface? Well maybe C is a general representation of the machine...I'll probably get nailed for this general/simple statement.
Yeah, well that tells use nothing. Please post the current code and error and warning
messages.
Try rewriting like so
for(temp=hl;(temp!=NULL) || (bOver1==true); temp=temp->next)
Try
std::vector<int> v1;
There's only dumb answers.
Could we see the definitions of the classes in your dynamic arrays?
In this task you don't have remove the vowels just don't write them to the new file. Just be sure to read the input file one character at a time. If the character is a vowel the do nothing, if the character is not a vowel then write it to the output file.
I just looked at your program...Your using a double for decimal, are you aware of the the storing characteristics of this type? Floating types may not store an exact value but a close approximate. This could be your problem.
I'm wondering...can you call this function and have fail on all the if conditions? What does it return in that situation? It should have a default returning true or false.
bool isprime(long denominator)
{
long max = long(sqrt(denominator));
if(denominator <= 1)
{ return true; }
if(denominator % 2 == 0)
{ return false; }
for( long counter = 2; counter <= max; counter++)
if(denominator % counter == 0)
{ return false; }
}
I tried compiling your program and got these warnings
In function ‘double lcm(double, double)’:
35: warning: no return statement in function returning non-void
In function ‘bool isprime(long int)’:
47: warning: control reaches end of non-void function
At global scope:
10: warning: ‘dividecounter’ defined but not used
lcm should be returning a double and isprime should be returning a bool.
I think your function is flawed..passname should be returning or updating its member data not another object's member data. It would make more sense to have this.
class Customers{
private:
char caName[kiArraySize]; //kiArraySize is 30
public:
//this is what I need help for
const char* passName() const;
};
const char* Customers::passName() const {
return caName;
}
void RandomFunction(Customers c){
cout << c.passName();
}
What can I say...this isn't a hacking forum.
create your case like this
case '1':
{
//put code here
}
Use braces so you can define variables in your case.
Try going at it like this...
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
char line[50];
int count = 0;
char c;
while((c = getchar() )!= '\n')
{
line[count++] = c;
}
line[count] = '\0';
printf("you entered: %s\n",line);
char *p = line;
char *start = line;/*save the start of a word*/
char *mya[100];
int argCount = 0;
while(*p)
{
if(*p == ' ')
{
*p = '\0';
mya[argCount] = start;
argCount++;
start = p + 1;/*next starting position*/
}
p++;
}
mya[argCount] = start;/*assign last one*/
fprintf(stdout, "%s\n", mya[0]);
fprintf(stdout, "%s\n", mya[1]);
fprintf(stdout, "%s\n", mya[2]);
fprintf(stdout, "%s\n", mya[3]);
return 0;
}
To print the table use a nested for statement...one for statement for each dimension.
But does that allow me to make it Bydimensional? I cannot see it... I mean:
I cannot print it as I want.
You can make the array as many dimensions as you like but remember that each element contains a structure that has both an int x and a int y. For printing try..Note for the demonstration I'll use a two dimensional example.
std::cout << "x->" << the_s[1][1]->x << " y->" << the_s[1][1]->y << std::endl;
Store the data in a structure
struct mys
{
unsigned int x;
unsigned int y;
};
struct mys the_s[10] = {{1, 2}, {3, 4}, {5, 6}, ...};
what about getting rid of the 0's is that possible that's what i have been asking?
I'm not sure what you mean by 'get rid of', your array is defined as a 10 x 12 array. If you require an array that is only known at run time them you'll have to explore the new and delete operators or possibly a vector.
Again, I ran the program you posted with the data you posted and it worked.
Here's my output...
20 24 32 35 40 50 50 45 40 35 30 25
30 38 40 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 30 25 20 15 20 25 30 40 45
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0