2 things:
a) Like someone already stated, replace A a[] with A* a and it will compile and run just fine.
b) Don't forget to delete *a in your destructor
2 things:
a) Like someone already stated, replace A a[] with A* a and it will compile and run just fine.
b) Don't forget to delete *a in your destructor
So, what exactly do you need help with? I don't see any questions on your part nor do I see any code that you may need help with so I have no idea what you want from us.
Programmersbook solution is exactly what you are looking for. Then you can use something like atof() to quickly change the string to a double if you need. Something like:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file;
file.open("Tbl_cliente.txt", ios::in); // open a file for input
file.unsetf(ios::skipws);
string Cli_nombre,Cli_apellido, Cli_dirr, compress, balance;
double Cli_total_compress, Cli_balance;
getline(file, Cli_nombre, '#');
getline(file, Cli_apellido, '#');
getline(file, Cli_dirr, '#');
getline(file, compress, '#');
getline(file, balance, '#');
Cli_total_compress = atof(compress.c_str());
Cli_balance = atof(balance.c_str());
//file >> Cli_total_compras;
//file >> Cli_balance;
cout << Cli_nombre << endl << Cli_apellido << endl << Cli_dirr << endl << Cli_total_compress << endl << Cli_balance << endl;
return 0;
}
What I did was this:
a) Create a new array that was larger than the first array.
int newSize = size + 3;
int* newPtr = new int[newSize];
b) Copy the old array into the new array
memcpy(newPtr, ptr, size * sizeof(int));
Basically it's just memcpy(Destination, Original, sizeInBytes)
Since an int is 4 bytes then we need to set it to (# of int * sizeof(int)) to get the right size data to copy.
c) Delete the original ptr and copy the newPtr to ptr
delete []ptr;
ptr = newPtr;
Now ptr is larger by 3 int (because newSize = size + 3). Then I just add 3 random digits to the end. The reason I used size, size + 1, and size + 2 was because I couldn't be sure what size would be originally. You could have chosen to enter 6 digits or 10 digits, right? So, I just did the lazy thing by saying is the first new position in the array, is the next free position, etc. If it were a longer example I would have done this part differently or it would get tedious very quickly.
#include <iostream>
using namespace std;
int main(){
int size;
cout << "How big is your array going to be?";
cin >> size;
int* ptr = new int[size];
// Enter numbers into array.
for(int i = 0; i < size; i++)
{
cout << "Enter Number: ";
cin >> ptr[i];
}
cout << "Numbers entered into array are: " << endl;
for(int i = 0; i < size; i++){
cout << ptr[i] << endl;
}
// Now i want to add more number to this array.
int newSize = size + 3;
int* newPtr = new int[newSize];
memcpy(newPtr, ptr, size * sizeof(int));
delete []ptr;
ptr = newPtr;
ptr[size] = 99;
ptr[size + 1] = 100;
ptr[size + 2] = 199;
cout << "New array looks like:"<<endl;
for(int i = 0; i < newSize; i++){
cout << ptr[i] << endl;
}
delete []ptr;
return 0;
}
Make a new array of larger size and copy the contents from one to another will do the job.
Also, note that using void* makes it very easy to make mistakes. I wouldn't recommend doing that. Use a proper struct, class, or union and you'll make life a lot easier for yourself.
If you want it to be an array you can do what Clinton was doing minus the mistakes. Try this code, it compiles just fine under gcc
#include <iostream>
using namespace std;
int main()
{
int a = 1;
double b = 2.0;
char c = 'c';
string d = "It works!!!\n";
void* array[4] = {&a, &b, &c, &d};
cout << "array[0] = " << *(int*)array[0] << endl;
cout << "array[1] = " << *(double*)array[1] << endl;
cout << "array[2] = " << *(char*)array[2] << endl;
cout << "array[3] = " << *(string*)array[3] << endl;
return 0;
}
OK, I see where the problem is. It's an easy one, but I missed it the first time I looked at the question.
if number1 > number2 then highest = number1.... and it stops checking right here. The moment one if statement is true then that's the end of the if/else if sequence and so it doesn't try to check past the first result that comes back true. You need to use individual if statements and not else if statements to make this work.
#include <iostream>
//include <conio.h>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
double total=0,num1,num2,num3,num4,num5,high,low;
cout << "Please enter number: ";
cin>>num1;
cout << "Please enter number: ";
cin>>num2;
cout << "Please enter number: ";
cin>>num3;
cout << "Please enter number: ";
cin>>num4;
cout << "Please enter number: ";
cin>>num5;
high = num1;
if (num2>high)
{
high=num2;
}
if(num3>high)
{
high=num3;
}
if (num4>high)
{
high=num4;
}
if (num5>high)
{
high=num5;
}
cout<< "The highest number is " << high;
return 0;
}
...nevermind...
There I am trying to display the random generators and the contents of the array next to each other... Is it incorrect to do it that way?
Thanks a lot
P.S I am still a beginner in C programming
x is your random number. So if x is 500 for example (I do realize that you've got it generating random numbers up to 6 so take this as as example) then you'll be trying to access A[500], but the array ends at A[4] so you'll get garbage output. What you want is to output from A[0] to A[4] so use the variable i and you'll find your program works just fine.
A[0] = random number 1
A[1] = random number 2, etc
while
A[random number] = ? (Who knows) ;)
printf("%10d%10d",x,A[x]);
Don't you mean A?
This line:
file.write((char*)&age,sizeof(age));
produces 4 blank spaces.
Well, I'm using Windows and it gives me an error when I try to compile your program. Hence, no file is created because the program is never run.
main MUST return an int in C++.
As soon as you change void to int and run the program you will notice that it does in fact create a file. If it matters I compiled it under both VC++ and GCC and both compilers created a file under Windows.
The company that produces the hardware only makes a Windows driver. That means that you can do one of a few things:
a) Petition the company that makes the hardware to make a driver for Linux
b) Wait till someone else writes a driver
c) Write your own driver, which would be very nice of you :)
d) Use another service that doesn't rely on this particular piece of hardware
Could you be any more general than that?
If you set the highest number to the first number, then why would you compare the first number to the highest to see if it's bigger?
high=num1
if (num1>high) high=num1;
It's not wrong, it just doesn't make much sense. If the highest number so far is number 1 then start comparing with number 2.
The code looks fine. What's the problem that you're getting?
Also, like jonsca wrote, if you use an array/vector and a simple for loop then you'll save yourself a lot of typing. (If you've gotten that far in the language, if not then portion of the program that you posted looks like you're doing things right.)
necrolin, thelamb thanks ..
But I still do not know what is the next step:
start with c++ and directx
or
start with c++ and easy library like SDL or GDK
I heard i must practice c++ and directx to gave me ability to program a game engine in the future.
p.s i studied math and physiques with c++ basics
I don't think that it really matters where you start as long as you start you'll be moving in the right direction. What difference does it make if you're writing games in one technology or another? The only difference that I see are your chances for employability. If you're concerned about getting a job in 4 years then look online at the classifieds and see what skills come up the most. Plus, any game programming that you do will give you skills that will be transferable to the next technology that you learn. As a game programmer you will always be learning the next new thing and in 10 years you may be working with tools that don't even exist now. My advice is choose a tool DirectX/SDL/whatever and make your fist game. That'll be a huge accomplishment in itself.
And like "thelamb" mentioned, you'll need to be skilled with numbers because you'll be seeing a lot of them.
If you want to make it really easy then just replace char aData with string aData and you'll be done. If you need to use a char array then you need to use a function like strcmp(charArray1, charArray2). There's a nice little reference on the function...
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Look at the examples on that page. They're very simple and match exactly what you are doing. If you follow the example then you'll be done.
Well, you know that we're assigning one value from your file to your array via: inFile >> data. So, as long as you put it in any loop then you're done. You can do it in a for loop if you want.
for (int i = 0; i < END; i++)
{
inFile >> data[i];
//Do some comparisons, output etc here
}
That's all there is to it.
Well, if your goal is to be a game programmer then my suggestion would be to start making games. I'm sure that every bookstore and most good libraries will have a whole section dedicated to game programming. Pick a good book up and start making games. My university has some courses in game programming (2 to be exact) so I'm sure that other schools offer courses on the subject as well, so depending on your age and education level you can always take a course.
Well, you want to have each element of the array store a single number, right? So you'll have to have at least one loop to go through the file and place the data into the array. You can use the for loop if your data is always the same length or a while loop if you have variable length data.
So, if you don't know how large the data section is, but that it will be less than some number... say 5000, then you can do something like this:
int main()
{
ifstream inFile;
inFile.open("data.txt");
int data[5000];
int current = 0;
while (inFile >> data[current] && current < 5000)
{
cout << data[current] << endl;
current++;
}
return 0;
}
Now, if you want you can add all your extra code within the while loop (or for loop... whatever you choose) and do your comparisons, write to the output file or whatever you want. So really, you only need one loop but you can do it in 2 loops if you really want.
My main point was that if you do something like:
inputFile >> data[100]
Then you're placing one single digit into one single location, location 100 to be precise. You're not putting the entire file into the array. If you're not convinced then try to print your array from your original code and you'll see that you get a bunch of garbage.
A character is a single character and not a string. 'a' is a character. 'Exit' is 4 characters 'E', 'x', 'i', 't'.
I guess you can do 3 things.
a) Compare 2 character arrays
b) Use a string
c) Use a single letter like, press 'E' to exit.
I don't understand what you're trying to do in the following line:
inFile>>house[elements];
But if you're trying to read all the data from the file into the array using that one line then it's wrong. You need to use a loop to enter the data into each element of house (from house[0] to house[end]) one number at a time.
I believe that <string.h> is the old C string header and is now called <cstring> in C++. This is completely different from the C++ strings library. In GCC (g++) you don't need to declare <string> and it will still compile. I guess that it must add it automatically for you, whereas in visual studio if you do not include it explicitly then your program will not compile.
Based on the book that I'm currently reading about C++ you shouldn't mix the old C style <string.h> with the new C++ ones <cstring>. Aparently this is because the new libraries are templated and the old ones aren't. This can mess up your programs although I can't remember any more than that at the moment. So, you should have declared:
<cstdlib>, <string>, and <iostream>.
Vectors can be a lot more convenient to work with than arrays because they're a lot more flexible. For example, you can resize them as you need whereas with an array you'd be giving it a default size when you construct it.
If you want to get really good at C++ you'll have to sink quite a bit of time into it. There's a lot to learn. If you want to work in C++ then I highly suggest reading up as much as you can on it. I definitely wouldn't wait till I got a job to learn how to program. You can always work on an opensource project to gain some experience. There's lots of great projects out there that need developers.
The information is right on their website. You need to use Windows to use this modem at the moment. There are no Linux drivers and there are no Mac drivers either. The problem, it appears, is that their hardware requires you to install a connection manager and right now the connection manager only supports Windows.
I suppose you could always try to install the connection manager via wine, but I have no idea if that would work for you.
You can use the editor "edit" if you like. Again, it won't be nearly as convenient as something designed specifically for programming, like Code::Blocks, but that's your choice.
As far as directories go I suppose there is nothing stopping you from using the tc directory. However, I believe that the best place for your files is to place everything in "My Documents". You can always place a directory like "Programs" or something in your documents folder and just compile everything from there.
Not sure if you have a compiler installed on your computer. If not, you'll need one to convert your code into an actual runnable program. The GCC compiler can always be downloaded from http://www.mingw.org/ if need be.
You don't "open" c++ in the command line. You edit your programs in a text editor like WordPad/Notepad or in an IDE (much better choice due to syntax highlighting and automatic indentation).
Try this IDE if you're just starting out: codeblocks
Just remember that you'll need to have not just the IDE, but also a compiler. You can download codeblocks with the compiler included. It's the option labelled: "codeblocks-8.02mingw-setup.exe" (where mingw is the Windows port of the gcc compiler).
You can then compile your programs right within the IDE or you can do so from the command line (so long as gcc is in your PATH) using: g++ myProgram.cpp -o myProgram.exe
Hope this helps.
Hi,
I hav heard that it isn't that simple to write a program in linux using c.
Lots of commands n stuff.Please simplify what actually I've got to do to run turbo c++ in my linux laptop as I am a beginner.
It's no harder to write C/C++ code in Linux than it is in Windows. In fact, I'd argue that for basic programming Linux is actually easier. You'll need a few things to get started. Open up whatever package manager you're using and install some of the following (depending on what you want to do):
a) gcc - C compiler
b) g++ - C++ compiler
c) An IDE or programmers editor. Some options are: codeblocks, geany (I really like this one), netbeans, or eclipse. The first 2 are the smallest and most basic ones. I'd probably start there.
Then just get a good C++ book and write as many programs as you can.
using
len = array.length( );
functionnn
As Crazyboy says, use the following:
string str (ARRAYNAME); SomeLengthVar=str.length();
or simply use:
strlen(ARRAYNAME));
That's about the easiest way
Did you read the question? He's asking for how to get the length of an integer array, not a string.:P strlen(MyIntArray) is just so not going to do what the poster wants.
using
len = array.length( );
functionnn
Correct me if I'm wrong, but length() works for strings not integer arrays. If it does work then please demonstrate how, because I got a syntax error trying it.
Here's some code that I know works. It shows 2 ways of getting the length. Wingless' answer is correct as shown bellow.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int myArray[] = { 1, 2, 3, 4, 5};
int length1 = sizeof myArray / sizeof *myArray;
int length2 = sizeof myArray / sizeof(int);
cout << length1 << endl;
cout << length2 << endl;
return EXIT_SUCCESS;
}
You have one of a few choices:
1. Convert your function from returning an int to returning a string
2. Convert MyName to an integer.... maybe something like MyID
3. Convert your function to return a void* and then cast it to a string...
Of these choices, option 1 is the best. You cannot return a string when you are specifically telling the program that it must return an int.
My ideas in code:
#include <iostream>
#include <string>
using namespace std;
string MyName()
{
return "Hubba Bubba";
}
int MyID()
{
return 1234;
}
void* MyName2()
{
static string name = "Hubba Bubba Jr.";
void* temp = &name;
return temp;
}
int main()
{
string name1 = MyName();
void* name2 = MyName2();
int ID = MyID();
cout << name1 << endl;
cout << *(string*)name2 << endl;
cout << ID << endl;
return 0;
}
Also, 1 more thing. When you print a double like 2.00 you'll get the output 2. So what you want to do is:
cout << fixed << setprecision(2);
This set's the precision of output to 2 decimal points and will print 2.00. A word or warning though, this will also print your integers (quantity etc) with 2 decimal points. So, you'll have to switch the precision between your integers and your doubles to get the output that you want.
Actually you declared total as in integer and you're using it to store the total price. So, it's not printing any decimals because integers don't have any decimal points in them.
Thanks for the explanation. Much appreciated.
My C++ course hasn't covered a topic yet, but I'm expected to solve a few problems with this material. I've got some working code I just don't fully understand it. Can someone help me, please?
void f(int** a) //What is this ** business? (1)
{
cout << **a; //prints the value stored in a, output is 45
cout << *a; //so what does this print? (2)
cout << &a; //confused here too (3)
}
//I'm OK with main(). I'm including it FYI
int main()
{
int i = 45;
int* ptr = &i;
f(&i);
}
I need help understanding lines (1), (2), & (3).
Thank you in advanced for your help.
Who says you can't because my C++ book says you can and my test program works fine too.
#include <iostream>
#include <cstdlib>
using namespace std;
class MyObject
{
string data;
public:
MyObject(string a) : data(a) {}
void print() const { cout << data; };
};
int main()
{
MyObject mo[4] = { MyObject("This "), MyObject("works "), MyObject("just "), MyObject("fine.")};
for (int i = 0; i < 4; i++)
{
mo[i].print();
}
return EXIT_SUCCESS;
}
You had several problems with your code. I got it to compile though.
A) void main() ... in C++ it's ALWAYS int main()
B) Check your opening & closing braces {}, that's where your mistake is. You have an unbalanced number of opening and closing braces so I believe that one of your functions is not ending where it should. To avoid this problem always use opening and closing braces like this:
for ()
{
for ()
{
for ()
{
....
}
}
}
C) BREAK; should be break;
D) exit() requires <cstdlib>. I changed your includes a bit to:
#include <iostream>
#include <cstdlib>
That's all you seem to need in this program.
Here's the code that compiles.... Note that you will want to double check it. I put braces where I "thought" you wanted to put them, ... aka verify that everything works right.
#include <iostream>
#include <cstdlib>
using namespace std;
int i, j;
int c[3][3];
void add(int a[3][3], int b[3][3]) {
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
c[i][j] = (a[i][j] + b[i][j]); //Addition of two matrices.
}
cout << "THE RESULTANT MATRIX IS:" << "\n";
for (i = 0; i < 3; i++) {
cout << "\n";
for (j = 0; j < 3; j++)
cout << " " << c[i][j];
}
}
void subt(int a[3][3], int b[3][3]) {
for (i = 0; i < 3; ++i) …
Hi necrolin,
You have been here awhile, I thought you should have known that this site can not give out information to over-ride passwords. We have no idea who's PC the poster wants to get into and what they want to do there.
I had no idea. Moderators, feel free to delete my post. I guess I'm a little too trusting of people. Sorry, if I broke any forum regulations. It wasn't my intention.
Do have have access to an admin account? As in any admin account? If so you can reset the password without knowing the old one via net user command.
net user administrator *
I got this to compile just fine by declaring the variables that I mentioned above.
For one, i & j aren't declared, unless they are global variables.... but my C++ teacher would kill you for doing that. Take a look yourself:
for (i=0;i<3;++i)
{
for(j=0;j<3;++j)
Should be: for (int i = 0 .....)
Also, where is c[][] coming from? It just appears in the middle of the function out of nowhere.
Also, this is wrong:
cout<<"Numbers entered: "<< numEntered<<endl; //output 10 inputed numbers
numEntered will only be the last number that you entered. Your array is where you are storing 10 numbers. So you'll need another loop.
for (int i = 0; i < 10; i++)
{
cout << number[i] << endl;
}
cin >> numEntered; //Ask for a number
//This part is fine, you get a number from the user
numEntered = number[i]; //oops, it's backwards
//should be number[i] = numEntered instead
Currently your code says:
numEntered is some user input
numEntered is some uninitialized data... when you want to be initializing your data instead.
#include <sstream>
stringstream toString;
int num = 55; //Your integer
string stringNum; //Your output
toString << num;
stringNum = toString->str();
toString->str(""); //Clear toString so you can use it again
This code converts an integer into a string. Hope it helps.
Here's some more reading on stringstream if you're interested:
http://www.cplusplus.com/reference/iostream/stringstream/
I temporarily removed the stringstream variable and the function that depends on it. It compiles great now. Problem solved.
Thanks for the replies.
Thanks, I haven't learned about copy semantics yet so I had no idea. I'm using that variable to convert some integers to strings so I will need it in the object, but I'm sure that now I can get it working. Thanks for the help. This was driving me nuts all evening.
#ifndef CLIENT_H
#define CLIENT_H
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
class Client
{
int id;
double lateFee;
std::string name, address, clientData, stringID, stringLateFee;
std::vector<std::string> movies;
std::stringstream toString;
public:
Client(int cId, std::string cName = "Default", std::string cAddress = "Default");
~Client();
void setName(std::string cName);
void setAddress(std::string cAddress);
void addMovie(std::string cMovie);
void addLateFee(double cLateFee);
void payLateFee(double cLateFee);
std::string getInfo();
double getLateFee() const;
};
#endif
In any case, I can create individual clients I just can't push them onto a vector without causing a compile time error. I have no idea what I'm doing wrong.
Compile time error...
manager.cpp: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/ios_base.h:738: error: `std::ios_base::ios_base(const std::ios_base&)' is private
manager.cpp:18: error: within this context
manager.cpp: In copy constructor `std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/streambuf:769: error: `std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
manager.cpp:18: error: within this context
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc: In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:238: instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Client, _Alloc = std::allocator<Client>]'
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/stl_vector.h:564: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Client, _Alloc = std::allocator<Client>]'
manager.cpp:18: instantiated from here
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/ios_base.h:741: error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:238: error: within this context
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc: In member function `std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >& std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::operator=(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/streambuf:776: error: `std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:238: error: within this context
This is my code:
#ifndef MANAGER_H
#define MANAGER_H
#include "dvd.h"
#include "client.h"
#include <vector>
class Manager
{
std::vector<Client> clients;
public:
Manager();
};
#endif
#include "manager.h"
#include "dvd.h"
#include "client.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
Manager::Manager()
{
//Works great
Client c(101, "Jill", "123 Sesame St.");
c.addMovie("Some Great Movie");
cout << c.getInfo();
//Compile time error
clients.push_back(Client(101, "Bill", "88 Whatever St."));
}