are you sure you don't mean:
Otherwise the return value is undefined (where it is not explicitly 0).
No its not undefined -- the last else statement will take care of that case. The line you added will never get executed.
are you sure you don't mean:
Otherwise the return value is undefined (where it is not explicitly 0).
No its not undefined -- the last else statement will take care of that case. The line you added will never get executed.
What compiler do you have? And what operating system. Your program gives me the same as the code I posted. It must be a compiler and/or os difference. Also, my computer has 5 Gig RAM.
str1 a str2 o
-14
Press any key to continue . . .
This is the output for apple and apparel
str1= le str2= arel
11
Press any key to continue . . .
BTY: I used VC++ 2008 Express compiler.
If you don't get the above output then you probably don't have the same program that I posted. Copy this, compile and run it to see what you get.:
#include <iostream>
using namespace std;
int str_compare(const char *str1, const char *str2)
{
if (*str1 != '\0' || *str2 != '\0')
{
if (*str1 != *str2)
{
if (*str1 > *str2)
{
cout << "str1= " << str1 << " " << "str2= " << str2 << endl;
return *str1 - *str2;
}
else if (*str1 < *str2)
{
cout << "str1 " << str1 << " " << "str2 " << str2 << endl;
return *str1 - *str2;
}
}
str_compare(str1 + 1, str2 + 1);
}
else return 0;
}
int main(int argc, char* argv[])
{
int x = str_compare("apple", "apparel");
cout << x << "\n";
return 0;
}
>> but what else can I do to put in CR and LF to the end of strings?
simple
char str[100] = "a string"; // see Salem's post #2 for this line
strcat(str, "\r\n");
I pass "Hella" and "Hello" as the two parameters, and it returned -14. The "Hello" for both and it return 0. "Hello" and Helloa" return 14.
What two strings did you pass it from main() ?
My suggestion: toss it into the trash can and get another computer. Or if you're the techi type you might try replacing the motherboard.
It'd be nice to know what operating system you have and what kind of computer (or is it your mother-in-law you are talking about :) )
worked ok for me, after I fixed the bug
int str_compare(const char *str1, const char *str2)
{
if (*str1 != '\0' || *str2 != '\0')
{
if (*str1 != *str2)
{
if (*str1 > *str2)
{
cout << "str1= " << str1 << " " << "str2= " << str2 << endl;
return *str1 - *str2;
}
else if (*str1 < *str2)
{
cout << "str1 " << str1 << " " << "str2 " << str2 << endl;
return *str1 - *str2;
}
}
str_compare(str1 + 1, str2 + 1);
}
else return 0;
}
I get the same error message that you reported, and I used Dev-C++ IDE instead of command-line compile. My suggestion for you is to split that project up and make a couple static libraries, or a DLL, out of most of those files. That way the linker doesn't have to deal with all those *.o files except as they appear in the archive libraries.
>>void main()
read this
As for your problem:
int main()
{
int sum[256] = {0};
ifstream in("filename");
char ch;
while( in.get(&ch) )
{
if(ch > 0)
++sum[ch];
}
// now just display all the rows in sum that are greater than 0
}
I think your meter's broken. Did you catch Palin's speech tonight?
WOW! She made a lot of good points.
On Obama :
"....this is a man who has authored two memoirs but not a single major law or reform - not even in the state senate."
"Victory in Iraq is finally in sight … he wants to forfeit."
"Terrorist states are seeking new-clear weapons without delay … he wants to meet them without preconditions."She has experience as a leader and an executive - what have Obama or Biden done other than be in Congress?
I think the race is going to get more interesting in the coming weeks.
Yes she made a decent speech, but she isn't running for President.
>>what have Obama or Biden done other than be in Congress?
Make the same statement about a lot of past presidents, most notably JFK and LBJ.
Q: how many presidents were neither senators nor governors?
A: here
If you don't want to do that then you can email it to me. PM me and I'll give you my email.
how did you declare it in your vb program? what you posted appears to be correct. But you don't need both the *.def file and use of _dllspec(__export) -- all you need is one of them.
zip up the project and attached it to your post. I'll give it a try. Make sure you deleted all compiler-generated files first to make the zip file smalller.
Great :) clearing temp files fixed the problem.
This must be the same issue as this thread because I just downloaded Firefox 3 and the menus appear normally.
I had to change computers because my other one bit the dust a couple days ago (64-bit Vista/IE7). Now when I log into DaniWeb I can't see the mod menus in Coffee House -- see thumbnail.
Yes, we can HELP you but we will not write it for you. You have to write the program then post questions about what you don't understand.
I believe the first thing you need to do is design the contents of the data file. What data do you want to save and in what format. Then you can begin designing the program -- what menus do you want, do you want to use structures of c++ classes to hold the inventory data. What data items will they contain (normally the same as in the data file). What questions do you want to ask the user? How will the user enter the data?
You need to think about all these things before writing even one line of code. Write them down on paper (or Notepad) to make the design easier and clearer in your head.
I just finished compiling and linking with Dev-C++ with no problems after adding the libraries.
[edit]Posted this before I saw your previous post (solution) [/edit]
I just compiled/linked with VC++ 2008 Express without a problem.
What was the exact error message? What you quoted was only an indication that one or more errors occurred.
Because the pointer has not been initialized to anything. It just points to some random location in memory, which your program may, but probably does not, own. Before using a pointer you must initialize it to point somepace. For example:
int *pVar;
int number;
pVar = &number;
*pVar = 9;
flush is a function so you need to use () cout.flush()
Try this macro #define PRINT(x) (cout << x << "\n" )
>> what would I name my file or doesn't it make any difference?
It doesn't matter -- name it whatever you want. Text files normally have *.txt extension, not *.dat.
>>So how do I have to call out this file in main?
With ofstream its not necessary to specify ios::out because that's the default. Same with ifstream. ifstream outMCSequentialFile( "calculations.dat");
That should tell you that nobody uses that editor. My advice is to scrap it and use something else to write your program.
The reason is because entering numbers leaves the '\n' (Enter key) in the keyboard buffer. cin.ignore() will just remove the '\n'. So you either need another cin.ignore() or do what you did and use cin.ignore(2)
I suppose you could use the time functions in time.h
time_t t1, t2;
t1 = time(0);
t2 = t1
while( (t2 - t1) < 1) // one second
t2 = time(0);
You might also check the Boost time functions
Typical official communications between Canadians and their neighbours to the South.
Americans: "Please divert your course 15 degrees to the north to avoid a collision."
Canadians: "Recommend you divert YOUR course 15 degrees to the south to avoid a collision."
Americans: "This is the captain of a US Navy ship. I say again, divert YOUR course."
Canadians: "No, I say again, divert YOUR course."
Americans: "This is the aircraft carrier USS Missouri. We are a large warship of the US Navy. DIVERT YOUR COURSE *NOW*."
Canadians: "We are a lighthouse. Your call."
That is very funny, but I hope you realize that Canadians and Mexicans are also Americans. America refers to the North American continent, not the USA.
how was the file opened? as text or binary? If binary then the next line character(s) depends on the operating system. In MS-Windows/MS-DOS you need "\r\n". *nix just "\n" and MAC "\r". If the file is opened in text mode then the operating system will make the appropriate translation of "\n" to whatever it needs.
I tried your three lines and it seemed to work ok for me. Opened the file with Notepad.exe and it looked ok (except for the Chinese characters). What editor are you using? If not English language then find out what it expects as line feeds.
This probably won't solve your problem, but ... Do you really need that vector? Why not just combine those two loops?
The function top() returns a reference to the top element of the stack.
http://www.cppreference.com/cppstack/top.html
It does not return NULL, but returns a reference to the first object in the stack. Checking for NULL is not a valid thing to do.
why? The operating system does all the thread swaping for you.
No I can't because I know nothing about that editor. Go back to where you got it and see if they have a user's group or forum where you can ask questions. My pure guess is that you failed to include some header file(s). Compare your program with the code you said that works to see what it is missing.
What operating system? Is it something that you are writing ? I did something similar in MS-DOS 6.X (a program with multiple context threads). In that program I had a kernel function (kernel to my program) that pushed all registers onto the stack, switch stacks, then poped registers of the new thread. This did not require any context id's
I have no clue what your swapcontext function does, other than swap out one thread for another.
Learn to program it yourself. Or offer to pay someone in the Looking To Hire board.
Welcome to DaniWeb.
>>i need input on where to start
Sart at the beginning by talking to your college school consoler.
Did you try something like this?
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
void foo(ifstream& in)
{
}
int main()
{
vector<ifstream*> streams;
for(size_t i = 0; i < 5; i++)
{
ifstream* in = new ifstream;
streams.push_back(in);
streams[i]->open("filename.txt");
}
foo( *streams[1] );
}
>>are STL containers faster then c-style arrays?
No. you can't get any faster access than c style arrays. But that isn't the point -- c++ STL eliminate many of the problems with c style arrays, such as dynamic array allocations. vector and other containers expand/contract by themselves, and they always know their size, unlike c style arrays.
you can use either sprintf() or strcat() to format the output file name. Example:
char outname[255];
char inputname[255];
char* ptr;
printf("Enter a file name\n");
fgets(inputname,sizeof(inputname),stdin);
strcpy(outname, inputname);
// truncate the extension
ptr = strchr(outname,'.');
if(ptr != NULL)
*ptr = '\0';
// now add new filename
strcat(outname,"_res.txt");
moved
The error message is pretty clear about what is wrong, and the solution sould be obvious: where did you declare MUSHROOM_END ???
1) 2) and 3). Very little, if any, performance gain. Those are not areas to optimize because the compiler will do that.
4) You talking about c++ classes? vertual class methods are extremly handy and I've used them often at my work (before I retired that is).
>>the virtual functions are especially to avoid.
That just a bold-faced lie.
5) >> I was always told to avoid using global variables
Yes, absolutely. Globals should be avoided whenever possible, but there are times when its not possible to avoid them. MS-Windows GUI programs (especially MFC) is a prime example.
>>usually after being told never to use gotos
Agree. NEVER EVER (almost anyway) use gotos. There is almost always a better way to create a branch.
>>Does the compiler now adays do this lowlevel optimization
yes, at least it tries to. Optimizing compilers will even toss out code that it thinks does nothing and move code around to make the program more efficient. Some compilers are better at that than others.
7) Depends on the situation whether to use dynamic arrays or not. IMO it is better to just use statically allocated arrays for small arrays. Its even better to use std::vector or similar containers in c++ programs than c-style arrays.
>>Does it make a difference, or is it just academia
Probably does make a very very small difference, but its mostly academic. Readability, clarity, and maintainability are significantly more …
all c and c++ programs have a main() function, and it has two optional arguments. int main(int argc, char* argv[])
argc is the number of command-line arguments (deliminated with spaces). argc is always > 0 because the first argument is the name of the executable program.
argv is a character array of the arguments. argv[0] is (usually) lways the program's name
To if you type c:>myprog Hello World <Enter key>
In the above, argc == 3,
argv[0] = "myprog"
argv[1] = "Hello"
argv[2] = "World"
.
That information is not in a *.ini file. Its in a database, such as MySQL and Microsoft SQL Server (there are lots of others too). The simplest kind of database is a plain text file. *.ini files only contain program configuration information that the user can change via an options menu, although it can contain other information too.
I don't have ubantu, but Microsoft VC++ 2008 Express reports 1 for the program that jamthwee posted. The compiler stored -61 in that byte. crappy *nix :)
>>I'm sorry, but I'm a beginner C++ programmer to such a "noob" level I don't know what you're talking about
UNICODE is a standard way to use non-English languages in computer programs. The standard UNICODE character is wchar_t, not char. Under MS-Windows wchar_t is defined to be unsigned short while in *nix (the last time I heard) it is unsigned long This is because many languages, such as Chinese, use graphic symbols which can be accommodated by wchar_t. In order to compile for UNICODE you have to set specific flags in the makefile -- I have no clue what those flags are for your compiler.
[edit]Considering jamthwee's test I would not bother with the UNICODE described above. It appears to be a compiler issue.[/edit]
Please post example code. Are you compiling for UNICODE ?
AFAIK Must be done in the constructor.
>>function(int *&head)
That is not a function that takes a 2d array. What you have there is a reference to a 1d array. They are not the same thing.
void functijon(int **array)
{
array[0][1] = 123;
}
The above is a 2 dimensional array of integers. The first dimension is an array of pointers. You still have to allocate the second dimension before the above will work
int main()
{
int **array;
array = new int*[10];
for(int i = 0; i < 10; i++)
{
array[i] = new int[20];
for(int j = 0; j < 20; j++)
array[i][j] = 0;
}
}