Could you put your question in context by giving us an example...
gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven
Could you put your question in context by giving us an example...
I'm making a program that accept 1 character long strings (and ignores spaces " ") until a user presses return/enter.
So far I have,
int main(){ char expression[1]; while ((scanf("%s",expression)) != EOF){ //code } return 0; }
it is assumed that the user is entering single digit strings (such as "a 1 2 3 d"(enter))
its seems to not like the while condition as it will keep accepting input even after I hit enter. I have tried using -1 instead of EOF but its the same output.Any ideas?
I don't think anyone mentioned the potential for buffer overflow in the original program...That's why I mentioned changing the variable from a character array to a single character.
Number one I would change your expression to:
char expression;
and then call a %c in your scanf.
#include <stdio.h>
int main()
{
char expression;
while ((scanf("%c",&expression)) != EOF)
{
fprintf(stdout, "char->%c\n", expression);
}
return 0;
}
The general rule is, for every time you malloc you have to call free..
Why set a pointer to NULL? Because if you call free on a NULL set pointer nothing happens, also its easy to check if a pointer is NULL by examining its value..
int *ptr = (int*)NULL;
if (!ptr)/*check if pointer is NULL*/
{
/*do something*/
}
Pointers, like everything else in programming should be left in a known/safe state.
Yes please post what you have so far. We appreciate a little effort on your part.
Yes, it is.
Then your going to have to figure out a scheme of swapping data to and from a file because you can't have an array bigger than your address space..
Your using
iResult-=iNumber;
Which loads iResult with the negative values of iNumber.
Ok those both look real simple, but I really need a return value of type string so I can call the function and set it equal to a variable to use in the API Dialog code.
I should have specified but I am still learning.
std::string ftostr(float myfloat) { char ans[20]; //float myf = 123.0 + (1.0 / 3.0); snprintf(ans, 20, "%f", myfloat); //std::cout << "ans->" << ans << std::endl; return ans; }
I tried that but my Code::Blocks compiler does not recognise snprintf. I know I am just fumbling around but at least I'm trying.
Did you include the cstdio library? If you want to return a string then cast ans as a string.
Try initializing these variables to zero...
int i, n, iNumber, iResult, iChoice;
Did you forget your semi-colon after your structure?
Remember your advancing your pointers as you copy into them...Your asking printf to print the end of your string.
The easiest way...Get on a 64 bit machine with a 64 bit C compiler.
So let me get this straight, you want to make an array greater than the virtual memory address space that can hold it? Is that correct?
It really depends, are you talking about a string object or are you talking about a c-string?
Try changing your function to:
int burcin(int x)
{
if (x==1) return 1;
else
return (x+ burcin(x-1));
}
Because you passing a copy of i to your function it will never increment the global i.
I mean the largest number of elements an array can have. Because of my program, when I declare
pid_t cpid[999999]
the program run well. But when I change the size of array to 9,999,999; my program gets segmentation fault.
pid_t cpid[9999999]
The largest number of elements an array can hold in C is whatever your implementation defines size_t as...Most implementation define it as unsigned int.
So it really depends on your implementation. Try doing a sizeof(size_t) to see how bytes it is.
No, I mean the max value that Index can have.
do you mean the max value of an array element or do you mean the largest element an array can have?
If you mean the latter then it depends what Index is? Is Index a char, int...size_t.
In the second example your creating an array of pointers which in your case are 8 bytes.
Do you mean what is the last element?
The last element is array[Index - 1].
Or do you mean how big can an array be? How many elements can it have....
C defines a type called size_t which accommodates the largest arrays.
Create your function like this
void increment( int *i )
{
++(*i);
}
and call it by passing the address of i
increment( &i );
Your copying your string over to a NULL pointer...It points to nothing.
Here's a simple way to make the conversion using snprintf().
#include <iostream>
#include <cstdio>
int main()
{
char ans[20];
float myf = 123.0 + (1.0 / 3.0);
snprintf(ans, 20, "%f", myf);
std::cout << "ans->" << ans << std::endl;
return 0;
}
Maybe you should post your code...
Please enclose your code with the proper tags
#include<iostream>
using namespace std;
struct BankAccount
{
int accountNum;
double accountBal;
double rate;
};
int main()
{
const int ACCOUNT = 5;
BankAccount info[ACCOUNT];
int sub;
double balTotal = 0;
double balAverage;
int accountNumber;
bool isFound = false;
for(sub =0; sub<ACCOUNT ; ++sub)
{
cout<<"Enter Account Number: ";
cin>>info[sub].accountNum;
cout<<"Enter Account Balance: ";
cin>>info[sub].accountBal;
cout<<"Enter Interest rate: ";
cin>>info[sub].rate;
}
for(sub=0; sub<ACCOUNT ; ++sub)
{
balTotal +=info[sub].accountBal;
++sub;
balAverage = balTotal/ACCOUNT;
}
for(sub=0; sub<ACCOUNT ; ++sub)
{
cout <<"Account Number";
cout<<info[sub].accountNum<<endl;
cout <<"Account Balnce";
cout<<info[sub].accountBal<<endl;
cout<<"Account Rate";
cout<<info[sub].rate<<endl;
}
cout<<"Total Account Balance"<<balTotal<<endl;
cout<<"Average for Account Balance"<<balAverage<<endl;
cout<<"Enter Account Number";
cin>>accountNumber;
for(sub = 0; sub < ACCOUNT; ++ sub)
if(accountNumber == info[sub].accountNum)
{
cout<<"Account Information :" <<endl;
cout<<info[sub].accountNum<<endl;
cout<<info[sub].accountBal<<endl;
cout<<info[sub].rate<<endl;
isFound = true;
}
if(isFound == false)
cout<<"invalid Account "<<endl;
system("pause");
return 0;
}
In C structures can hold pointers which can point to functions...So in a way, C structures can hold(point to) functions..
I didnt know this. Does this mean I should use execve(2)?
No it means any function from the exec() family will replace the current process...You can use fork() to split the process and then call a exec function in the child process or just use system().
I would investigate system() first..
Please read the click below
The exec() family of functions replaces the current process image with a new process
image. The functions described in this manual page are front-ends for execve(2).
(See the manual page for execve(2) for further details about the replacement of the
current process image.)
This exert is from my help Linux files.
Hmm thanx gerard I get it . But why their is an extra h with quotes in the output.
If i write char *c="hello" . Their is no extra h and " in the output.
Can you please give the reason for this.Thank a lot in advance.
When you call c with std::cout << c << std::endl; it will try to keep reading/displaying until it finds a '\0'. Why is it displaying the extra 'h'? Its probably the next printable character it found....On my system it printed a whole string of nonsense before it terminated with a '\0'.
Here's my output
0x7fff8fbb9faf A
A����� A
std::cout is interpreting c as a character string...You know something that ends with a '\0'.
If you want the address try this:
std::cout<<(void*)c<<" "<<*c<<std::endl;
The C programming Language..
Well you have this in your code at line 25
int getCenturyValue (int year);
int getYearValue (int year);
int getMonthValue (int month, int year);
weekday = (day + getMonthValue + getYearValue + getCenturyValue) + 7*((day + getMonthValue + getYearValue + getCenturyValue)/7);
Your calling functions without passing any values to them....i.e.
getMonthValue requires that you pass two integers to it like
int value1;
int value2;
getMonthValue(value1, value2);
Line 46 is an open curly brace...Are you sure your braces matched?
This might move along faster if you posted specific problems..
I didn't notice an if statement.....It could be said that was the only thing that didn't have any errors...What compiler are you using?
#include <iostream>
#include <cmath>
using namespace std;
int main (void)
{
float Height, Radius, BoxHeight, BoxLength, BoxWidth, SiloVolume, BoxVolume, BoxVolumesum = 0, Grainleft;
char choice = 'y';
cout << "Enter height of silo." << endl;
cin >> Height;
cout << "Enter the Radius of the silo." << endl;
cin >> Radius;
SiloVolume = 3.14 * pow(Radius, 2) * Height;
BoxVolume = 0;
while ( choice == 'y')
{
cout << "Enter Height of box car." << endl;
cin >> BoxHeight;
cout << "Enter length of box car." << endl;
cin >> BoxLength;
cout << "Enter the width of box car." << endl;
cin >> BoxWidth;
BoxVolume = BoxHeight * BoxLength * BoxWidth;
cout << "Would you like to add another car? Type y if yes, or any other key for no.";
cin >> choice;
}
BoxVolumesum = BoxVolume + BoxVolumesum;
Grainleft = SiloVolume - BoxVolumesum;
cout << "You will have " <<Grainleft<< " grain left." << endl;
return (0);
}
For future reference...Please adopt a neater coding/posting scheme, something like the above code.
You were close...
#include<stdio.h>
int main (void)
{
int row,col;
int num;
num=10;
row=0;
col=0;
while(row<=num)
{
row++;
while(col<row)
{
col++;
printf("* ");
}
printf("\n");
col = 0;
}
return 0;
}
Take this out of your header file
long addresses[5];
and put it in your main executable...
When you compile def.c you create a label called addresses and when you compile mainfile.c you create another label addresses. When you bring both files together for the final executable it fails because addresses is defined in both def.o and mainFile.o.
It means
(*filterdata).Q_angle = Q_angle;
Could you post a brief example of the data files...
You can use EOF by pressing Ctrl-Z in Windows or Ctrl-D in Linux....Here's an example
#include <iostream>
#include <string>
int main()
{
std::string word;
std::cout << "Please enter a string of words...EOF to exit" << std::endl;
while ( std::cin >> word )
std::cout << "program recieved->" << word << std::endl;
std::cin.clear();
return 0;
}
i'm a complete rookie
Then don't get Accelerated C++ as a first book...as a second yes but not as a first..
I think you can still get "Teach Yourself C++ in 21 days" by googling...The book is a bit dated but its still a good read for the rookie.
thanks for your help, could you reccomend any good c++ books?
Depends...Are you a complete programming rookie or do you know how to program in other languages besides C++.
What is
Info record;
this
'monday'
should be
"monday"
Strings are enclosed with double quotes not single quotes...
If your learning C++ then please get yourself a good book on the subject...Tutorials just don't cut it.
I'm not sure what your trying to accomplish here...Do you want to read each character and display it on its own line?
I quickly wrote this. It will output the characters like you posted
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char**argv)
{
char ca;
FILE *fd;
if (!(fd = fopen(argv[1], "r")))
{
fprintf(stderr, "could not open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while (fread(&ca, sizeof(char), 1, fd))
{
if (ca != '\n' && ca != ' ')
{
if (ca == '*')
{
fprintf(stdout, "%c", ca);
}
else
{
fprintf(stdout, "%c\n", ca);
}
}
}
fclose(fd);
return 0;
}
Try
ReadFile.read((char*)p_ReadBuff, 1);
Yeah I would try
unsigned char m_ReadBuff;
unsigned char *p_ReadBuff = &m_ReadBuff;
Try using unsigned int...How is ReadBuff defined?
If your using strings...why won't you just
string contentsToAdd += inventory[counter].itemName + " ";
Try using something like:
std::string mystr = "the string";
mystr.c_str();//to return a C style string
Are you talking about something like below?
#include <iostream>
#include <string>
#include <cstdio>
#define ARR_SIZE 4
struct mystr
{
std::string mystr;
int myint;
};
int main()
{
char *str = new char[10];
str[0] = '\0';
std::string ans;
struct mystr thestr[ARR_SIZE];
thestr[0].myint = 1;
thestr[1].myint = 2;
thestr[2].myint = 3;
thestr[3].myint = 4;
thestr[0].mystr = "the first";
thestr[1].mystr = "the second";
thestr[2].mystr = "the third";
thestr[3].mystr = "the fourth";
for (std::string::size_type i = 0; i < ARR_SIZE; ++i)
{
sprintf(str, "%d", thestr[i].myint);
ans += (std::string)str + " ";
}
std::cout << "ans->" << ans << std::endl;
return 0;
}