got it all sorted out...thanks for the help!
You are welcome .... Mark this thread as solved
got it all sorted out...thanks for the help!
You are welcome .... Mark this thread as solved
Why are you declaring a vector of Persons as well as an array of Person ? You need to have one not both
The way you are storing the best friends name is wrong. As of now you have a string called best friend in the main function. The getline call stores the input in this string. I dont think this is what you wanted. Because of this the display function gives a seg fault
You are using fread for reading from a file ... Check out the suggestions posted in the posts above
There are 2 -3 compile time errors in your code..
#include<iostream> #include<vector> #include<iomanip> #include<string> using namespace std; class Person { private: string name; // Make this bestFriendName. You have used bestFriendName in the function definitions Person* bestFriend; int count; public: void setName(string n) { name = n; } string getName() { return name; } void setBestFriend(Person *p) { // This should be p not b bestFriendName = b; } Person* getBestFriend() { return bestFriendName; } void setCount() { count = count + 1; } void display() { cout << "name: " << name << endl; cout << "bestfriend: " << (*getBestFriend()).getName() << endl; cout << "popularity: " << count << endl; } }; int main() { string yourName = ""; string bestFriendName = ""; int number = 0; bool done = false; vector<Person>persons; Person myPerson[20]; cout << "Enter name (-1 to stop): "<< endl; getline(cin, yourName); while(yourName != "-1") { // No need for this line. Memory is already allotted when you create the array of objects myPerson[number] =new Person; myPerson[number++].setName(yourName); cout << "Enter name (-1 to stop): " << endl; getline(cin, yourName); if (yourName == "-1") { for(int i = 0; i < number; i++) { cout << "Enter my best friend of" << myPerson[i].getName() << endl; getline(cin,bestFriendName); } } } for(int i = 0; i < number; i++) { myPerson[i].display(); } system("pause"); return 0; }
This should clear your compile time bugs... But there are logical bugs in your code. When I ran your code I …
If you the format of the data then you can use the fscanf function
If not use read to read the contents of the file into a char array and then implement a parser to parse the data
Dude Where are your code tags
I am reading your getCalendar date function to print the date in the proper manner, but just a quick question, cant you read the file line by line and into a char array and then use strtok with space as the separator ? From what I read in the problem statement and the way the input is given to you I am pretty sure it will work ...
Can you post little more of your code ... These are the things that I want to see ..... The way you have written the for loop and the way you have written the conditions in the if else statements ....
PS are you sure there is no infinite loops any where else ?
@Banfa ...
When you calculate new number of trucks and load per truck why will you get a different value ?
All the choice should be inside the for loop. From the code that you have posted, your for loop ends after the first if statement.
Also for your program it would make sense to change the data type of choice to int as opposed to what you have now
If you are looking for a direction to get started at least .... Try making a text editor first which has the basic functionality like Notepad ....
Once this is done (and you are still determined to move ahead) then you can think of more advance functionality
1. In the code that you have posted, you have defined the function read in the function main ..... Put the brackets properly
2. Check how you have called the read function. Is that the correct way to call a function
3. I would recommend changing the name of the function from read to read_file or something of that sort . Read is a in built function to read data from a file... You may get some messy errors later. Why take chance ?
I do not think that the logic that you have written for your premium bill is correct. Probably your premium bill function should be some thing like this
bAmount= basic_cost ;
if( day_mins_usage > day_mins _free)
{
// Add the extra charges here
}
if(night_mins_usage > night_mins_free)
{
// Add the extra charges
}
I hope you understood the mistake
On line 51 why are you closing the server socket ? Should that not be like the last step of the program ?
In the main function case 'r' you are returning the value from the function in the variable AmountDue but you are printing the variable bAmount
This is not what you have done in the next case
Also line 75- 79 check where you have put the ' { '
Is that the correct place ?
char *letter;
if(info[i].grade >= (mean + 1.5*stddvt)){
info[i].letter= "AA";
This code is wrong. When you say char* letter you just have a pointer. For storing data the pointer has to point to some memory. Check out the function malloc
I tried changing the code to:
void AddDevice(int Foo, unsigned int Bar); unsigned long* ptr2data; DWORD temp; *ptr2data = (unsigned long) SomeArrayOfData[4]; temp = (DWORD) ptr2data; AddDevice(7, temp);
as suggested by Jephthah, and it didn't work :(
The method suggested by Deeep wasn't very clear, I tried changing my code to:
void AddDevice(int Foo, unsigned int Bar); unsigned long* ptr2data; DWORD temp; ptr2data = (unsigned long*)&SomeArrayOfData[4]; temp = (DWORD) &ptr2data; AddDevice(7, temp);
this didn't work either :(
Please help me if you have any more suggestions or see what I have done wrong.
Thanks
The variable temp contains the address of the 4th element of the array. When you pass this variable I think you want to pass it this way
AddDevice(7, *temp);
Right ......
I have given you the logic .....Make the correction on your own
Your program is wrong
This is the O/P of your program
4
5
5
6
6
7
7
7
7
8
8
8
9
9
9
9
9
10
10
10
10
11
11
11
11
11
11
11
11
12
12
12
12
12
13
13
13
13
13
13
13
13
13
13
14
14
14
14
14
14
15
15
15
15
15
15
15
15
15
16
16
16
16
16
16
16
17
17
17
17
17
17
17
17
17
17
17
17
17
17
18
18
18
18
18
18
18
18
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
20
20
20
20
20
20
20
20
20
There is very little difference between those 2 questions .....
Think how can you use my explanation to solve your problem
This is a basic algo for checking if a number is prime or not
Store the number in a
Loop: From i= 2 till i= sqrt(a)+1
if(a%i==0)
break
else
Do nothing
if(i==sqrt(a)+1)
This means that no number between 2 and sqrt(a) is a factor for a. which means a is a prime number
else
a is not a prime number
Now You can extend the above logic to solve your problem
Hey Thanks...and how to calculate remainder of
a
andb
only ifb
is one less thana
??
i mean when the values ofa
andb
are same then the remainder must not be calculated ?
Put an if condition
if(/*check if b is less than a */)
{
// Calculate the remainder
}
else
{
// Do nothing
}
Are you printing on the server like this ?
printf("Calender server has TCP Port %d and IP address %d ", SERVERPORT); // server IP is missing
Then the problem is that printf has just one argument but 2 "%d", so the first %d will print SERVERPORT and the other one will print garbage
You will notice that in the server you have written a while(1). This means that the server can serve an infinite number of requests. If you want to just server any 2 requests then change the while(1) to for(i=0;i<2;i++)
It stores it in argv[1] if you execute the program this way
./client TIME
It stores it in argv[2] if you execute the program this way
./client localhost TIME
Why did you think TIME would be stored in the buff array ?
Thank you a lot.. I already fix the problem in line 66 & here is my new client codes
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <time.h> #define PORT "21708" // the port client will be connecting to #define MAXLEN 100 // max number of bytes we can get at once // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(int argc, char *argv[]) { int sockfd, rs, ss; char msg [80]; char buf[MAXLEN]; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; if (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); } memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo("nunki.usc.edu", PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and connect to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; } break; } if (p == NULL) { fprintf(stderr, "client: failed to connect\n"); return 2; } inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); freeaddrinfo(servinfo); // all done with this structure if ((ss = send(sockfd, buf, MAXLEN-1, 0)) == -1) { // send query perror("send"); exit(1); if ((rs = recv(sockfd, msg, MAXLEN-1, 0)) == …
Put a while loop in the calculate function.... Loop till left over amount has become 0.
One way could be to put a if condition before the mod operation. If c is equal to a, dont do the mod operation. Else go ahead and do the mod operation
There are 2 big mistakes in your program
1. In the printf that you do on line 66 in the client program is causing a seg fault because of parameter mis match. Comment out that line or put the proper parameters
2. The server is waiting for the client to send Time/ Date. If nothing is received the server just waits without doing anything. But your client does not send that data.
Have you included math.h ?
Can you post the code where you have actually used the ceil function
right, i remove the ;
can u give me a example how declare in my header the function and how write the code in other file?
plz!
My previous post answered this question. But I guess for your application, you can use your method
Make a header file.... Say file1.h. Include all the header file and function declarations is this file. For ex
#include<stdio.h>
#include<string.h>
void display(int,int);
Then make a .c file. Put all the function declarations in this file. Include the header file, file1.h in the source code file
#include "file1.h"
int display(int a,int b)
{
// Your code here
}
int main()
{
display(1,2);
}
When you declare some thing of the sort
int int1;
The compiler gives any arbitrary value to i. This could be 0,1,2,... any value what so ever.
As you input a char in response to the second cin, the compiler ignores the value entered by you.
Later when you want to display the entered value, the compiler prints the value of i, which will be garbage in your case
Run this code. If you give the input AZ, it will print A-1
int main()
{
char ch1;
int int1=-1;
cin >> ch1;
cin >> int1;
cout << ch1 <<"\n";
cout << int1 << "\n";
getch();
return 0;
}
I hope this clears your confusion
When we multiply 2 matrices, each element of the first row of the first matrix is multiplied with each element of the first column of the second matrix and so on
If you can bring one row of the first matrix and one column of the second matrix into the main memory, multiply them and store the result back to the disk, this might speed up the computation
I want my code to take two characters and print them, but this code only takes one character, prints it and terminates, can you please tell me what's wrong with it? Thank you.
#include <stdio.h> int main() { char a, b; scanf("%c", &a); scanf("%c", &b); printf("%c %c\n", a,b); return 0; }
When you enter the first character you are actually entering 2 characters. The character itself and ('\n') when you hit the enter key. So both the scanf's get satisfied. The solution is to use fgets as suggested above
Quick question... How are you supposed to edit the test ?
Google for getch()/ cin.get()
Put it before the last cout
Where are you getting the crash ? I ran the same code and I got the correct output. This is my output
abcdefghijklmnopqrstuvwxyz
ajluz
0bcdefghi0k0mnopqrst0vwxy0
bcdefghikmnopqrstvwxy
Did you put a getch() after the last cout ?
You can do something of this sort
class test
{
int x;
public:
void getNum();
void printNum();
};
void test::getNum()
{
// You do not have to pass any parameters to this function. This is a member function of the class hence it can access all the members of the class
cout<<"Enter x\n";
cin>>x;
}
void test::printNum()
{
// You do not have to pass any parameters to this function. This is a member function of the class hence it can access all the members of the class
cout<<x;
}
For each object the member function can access all the data members of the class
One solution could be to replace the '0' by ' ' ie the space character. Is this allowed ?
Make a temp array and run a for loop over the alpha array and each time the character is not a (0 or ' ') copy it to the temp array.
You do not need to make different functions for each object. Also your print function is wrong
Read up the concepts of objects one more time and you will see that you just need one getter / setter methods per class
PS: Change your void main to int main
If after resolving the memory issue you are still getting file not found, I will suggest that you take the full path name of the directory
For printing the string the normal way (unreversed) you can use
printf("%s\n,name_of_string)
Also I would advise clearing out the contents of array1 and array2. As these arrays are local variables they will contain junk values and this could affect your out put
Just take any one skill and become the master of that
In the main function you call print on line 107.. Before that can you print the entire array of structs ??
Some thing of this sort
struct test
{
int x;
}t1;
t1.x=20
This might get you started.
There are 2 string: stringA= hellow , stringB= owl
So what you got to do is take the first character of string B and iterate it over stringA from the ending. In this case, we take the character o from stringB and compare it with w
It does not match so move ahead to o. This one does match. So now you compare the 2 character of stringB that is w
Did you get the logic ?