Close -- read the instructions more carefully
#include <iostream>
using namespace std;
struct phonebook
{
char firs_name[20];
char last_name[20];
char email[20];
char phone_no[10];
};
int main ()
{
}
Close -- read the instructions more carefully
#include <iostream>
using namespace std;
struct phonebook
{
char firs_name[20];
char last_name[20];
char email[20];
char phone_no[10];
};
int main ()
{
}
You can not use != or == operator to compare two character arrays, you have to call strcmp() to do that if( strcmp(A.signup.username,A.login.username2) != 0)
you have to typecast either the numerator or denominator to float or double average[i]= (double)((wins[i]*100))/(wins[i]+losses[i]);
Put $5,000.00 USD in my PayPal account and I'll write it for you.
>>With the two additions, it skips to the end after I enter the number of numbers
Because scanf() leaves the Enter key '\n' in the keyboard buffer, and fgets() stops reading at the first '\n' it encounters. Call getchr() after scanf() to remove '\n' from the keyboard buffer.
>> just see documentation of math.h or cmath copy and paste the fucntions for sqrt and done.
The code for the functions isn't in math.h, only function prototypes. So copying them is little or no value.
Of course you could always write your own ln function.
you can use getline() with the last argument being the comma instead of the default space. getline(ins,Lastname,',');
line 54: for(int i=0; d != 400; i+10)
The condition d[i] != 400
is never met. Probably should be i != 400
or better yet i < 400
Use whatever version is supported by the computer's hardware.
That was my original thought too, but I dismissed it because it would be horribly sloooow.
If you want individual averages then just correct the calculation in line 55 of your original post.
The while statement on line 54 is not going to work correctly because it will cause the lzst record in the file to be read twice. One way to fix that is like this:
int numRecords = 0;
while( ReadRec(inFile, site) )
{
++numRecords;
// blabla
}
ifstream& ReadRec(ifstream& inFile, webRec& temp)
{
inFile >> temp.url >> temp.revenue >> temp.hits;
return inFile;
}
well, then just change the formula in the code I posted
int total_wins = 0;
int total_losses = 0;
for(int i = 0; i < numItems; i++)
{
total_wins += wins[i];
total_losses += losses[i];
}
float average = (float)total_wins/(float)(total_wins + total_losses);
Do you know how to allocate arrays?
Do you know how to read a file?
If not, then you need to study them before doing the rest of this program.
>>average= (wins[numItems]*100)/losses[numItems];
That will crash when losses[numItems] is 0 because division by 0 is undefined. Test losses[numItems] for 0 before performing that division.
I think the whole algorithm in that function is incorrect. To get the average you have to sum them all up and then perform the division
int total_wins = 0;
int total_losses = 0;
for(int i = 0; i < numItems; i++)
{
total_wins += wins[i];
total_losses += losses[i];
}
if(total_losses == 0)
total_losses = 1; // prevent division by 0 error
float average = (float)total_wins/(float)total_losses;
You could do something like this
#include <fstream>
#include <iostream>
#include <string>
class MyClass
{
public:
std::ostream& foo(std::ostream& stream, std::string msg);
};
std::ostream& MyClass::foo(std::ostream& stream, std::string msg)
{
stream << msg << '\n';
return stream;
}
int main()
{
MyClass m;
m.foo(std::cout, "Hello");
}
>> Is there a way to get these numbers to have zeros prefixed to them?
Not that I know of
(*m_log) << "message;
The parentheses are not necessary. Just do this: *m_log << "message";
You could just google for the file name, such as google for "sys/types.h", or just use a text editor to view the file that is stored on your computer. Or ... buy a book that contains all the information.
You can sort the vector without splitting it. call std::sort to do the sorting. All you have to do is to expand the resource number from 1 to 2 digits, such as "resource1" to "resource01" when you read them from the file. That way std::sort (or any other sort algorithm) will sort the vector correctly.
I got the same question in a PM, which I promptly just deleted.
The string is null terminated. strcat() properly appends a null character to the destination, so the only issue is making sure that the string is null terminated before the first call to strcat().
I knew that -- I was just testing you to see if you did too :) :)
>>ReadDir4.cpp:44: error: ‘struct TFile’ has no member named ‘d_name’
Lear how to recognize the errors in your program. This one is pretty simple and straight forward. Look at the declaration of TFile, located at the top of your program. Notice that is does not have a member named d_name. Now look at line 44 of your program, where the error was issued. Fix the problem there by replacing stat(filedata.d_name.c_str(),
with stat(filedata.name.c_str(),
.
After fixing that recompile and do something similar with the remaning errors.
Learn to use your compiler's debugger so that you can single-step through the program and find out why it isn't working. What compiler are you using?
line 316: After closing both files, instead of reopening them and copying the new data back it would be a lot easier and quicker to delete the original file and rename the temp file to the name of the original file. No copying necessary.
Your program seems to work correctly for me on Windows 7 and using VC++ 2010 Express.
You need to write code in the OnClick event handler. While in the designor just click the button and the IDE will generate an OnClick event handler for you.
not that hard, just locate the first numeric digit then copy both parts to new char arrays
char str[] = "staff101";
char s1[6], s2[6];
strnapy(s1,str,5); // copy first 5 characters
strcpy(s2,&str[4]); // copy remaining characters
>>any idea guys if c++ can generate pdf
See this thread
font size can not be changed in console programs. You have to write a GUI program to do that.
All that's necessary is to set the first character to '\0' to make it a valid string for strcat():
p = malloc(n); p[0] = '\0';
strcat() will then ensure that the result is properly terminated for printf().
The string still must be null-terminated, which is why I suggested flooding the entire buffer with '\0' before doing anything. And you need to add 1 to n in the malloc line.
after allocating the array you have to initialize it to 0 so that strcat() will work correctly the first time. You also have to null-terminate the array after the loop is finished so that printf() will work correctly. Both issues can be solved at the same time by calling memset() to flood the array with '\0' bytes.
p = malloc(n+1); // make room for null-terminating character
memset(p,0,n+1); // initialize the array
The posts in this thread are in backwards order (newest post first). I looked at other threads in which I did not contribute and they appear in normal order (oldest post first).
The creation times of the two posts are reversed.
The compiler did not create the dtmf.exe file for some reason. To compile the program as C all you have to do is rename the file dtmf.c instead of dtmf.cpp
CSV format is pretty simple -- each record is separated by '\n', as it is in almost every normal text file. Each field is separated by either a comma ',' or a tab '\t'. Which one you use is up to you and might depend on the data to be stored, and you can't mix the two. Text strings are surrounded with quotes ", while numeric data is not surrounded with anything
Example: "Once upon a time",1,23.45,"there were three little pigs"
line 25 is using the wrong variable. display TEXT, not search.
>>I'm curious as to what you meant by the problem with my while loop. Did you have an idea that might of been easier?
Yes -- its in the code snippet I posted.
Not all 255 possible ascii characters are printable and you will just see blanks or funny looking squares for them. Here is one way the table should be printed.
line 87: pWalker is an uninitialized pointer and your program is bound to crash. You need to initialize it with the value of the parameter pList. As for the problem you asked about, please tell us which line in the code you posted the error occurs.
The loop starting on line 38 never increments the i counter, causing everything to be read into the same element of all the arrays.
The errors you posted have nothing to do with the libraries because the compiler has not gotten that far yet.
The while loop is incorrect -- make sure currentPtr != NULL before this loop starts!
while( currentPtr->next != NULL)
{
// blabla
}
currentPtr->next = newPtr;
How its coded is obviously depend on the compiler. The version you posted is almost identical to the one that is included with VC++ 2010 Express. This is from <xutility>
// ALGORITHM STUFF (from <algorithm>)
// TEMPLATE FUNCTION max
template<class _Ty> inline
const _Ty& (max)(const _Ty& _Left, const _Ty& _Right)
{ // return larger of _Left and _Right
return (_DEBUG_LT(_Left, _Right) ? _Right : _Left);
}
And that, of course, is how std::max is coded.
why didn't you make both parameters the same type? T max(T t, T u)
Since the class has no public members it can't be used in main()
But to answer your question, it is common practice to have multiple constructors as long as they have different parameters. Only one constructor will get called, the one with the same type of parameters as the calling function. In the case you posted, the first constructor will be the one that gets called.
>>Also why is the argument of 2nd constructor int *a instead of int a[] .
Yes there is a difference -- int *a
is a pointer to something, it may or may not be an array of integers because it also could be a pointer to a single integer. int a[]
is definitely an array of integers. Whether you use them both to refer to an array of integers is up to you
The problem is just the came as in any other c++ program -- the global method must have an instance of the c++ class in order to call one of its methods, unless the c++ class method has been declared as static.
class foo
{
public:
static int static_method() { return 0; }
int another_method();
}
void gmethod()
{
foo par;
foo::static_method(); // call a static method
par.another_method(); // call a non-static method
}
You are not saving the file contents, your program just reads the file and tosses away the information.
Turn the program around, first prompt for user name and password, then read the file, making the comparison for each line of the file.
call getchar() to read the file one character at a time, something like this
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE* fin;
int a, i;
char myString[5];
fin=fopen(argv[1],"r");
fscanf((fin,"%d", &a);
for(i = 0; i < a; i++)
myString[i] = (char)getchar();
mystring[i] = '\0';
printf("%s\n", myString);
return 0;
}
>> foundDecade(string decade)
what have you tried ? Since each instance of Celeb::name contains both name and decade just a simple search for some name will not work. To make searches less complicated you might want to split Celeb::name into two separate items: name and decade.
class Celeb
{
private:
std::string name;
std::string decade;
...
...
With that change the foundDecade() method just becomes
bool foundDecade(string dec)
{
for(int i = 0; i < numCelebs; i++)
{
size_t pos = celebs[i].decade.find(dec);
if( pos != std::npos)
return true;
}
return false;
}