Ok, thanks for the tips s.o.s :!:
Ok, thanks for the tips s.o.s :!:
Hi all,
Lerner, thanks for those tips, I'll try to remember and use them in my future projects, however small they are !
I'll give the pseudo code from WaltP another try and see whether I can get it to work using that, thanks for the help WaltP !!!
Ive finished the exercise which went as follows:
- Read a sequence of words from input. Use Quit as a word that terminates the input. Print the words in the order they were entered. Don't print a word twice(had major difficulty with this(thanks to Lerner and s.o.s for the help). Modify the program to sort the words before printing them.
#include <iostream>
short input(char arr[][10], const short);
void compare(char arr[][10], const short);
void sort(char arr[][10], const short);
int main()
{
const short SIZE = 10;
short amount = 0;
char wordArr[SIZE][10];
amount = input(wordArr, SIZE);
compare(wordArr, amount);
std::cin.ignore(2);
return 0;
}
short input(char arr[][10], const short SIZE)
{
char tempStr[10][10];
short amount = 0;
for(short i = 0; i < SIZE; i++)
{
std::cin >> tempStr[i];
if(strcmp(tempStr[i], "Quit") == 0)
{
strcpy_s(arr[i], tempStr[i]);
return amount;
}
else
{
strcpy_s(arr[i], tempStr[i]);
amount++;
}
}
return amount;
}
void compare(char arr[][10], const short SIZE)
{
bool contr = false; //declare and set a boolean variable to act as a flag
sort(arr, SIZE); //call the sorting function before output
//for each word in wordArr after first word and call it current word
for(short i = 0; i < SIZE; …
Instead I would do something like this:...
Yep, that did the trick Lerner, it works, only thing I had to change is the initialization of the first word like this:
//first word hasn't been printed before so it will always be printed
short i = 0;
std::cout << wordArr[i] << std::endl;
If I didn't do that, the first word wouldn't be printed, instead, junk that was in the array would be printed.
Thanks for that example.
Hi Walt,
When trying out your pseudo code like this:
#include <iostream>
int main()
{
char words[5][10];
for (size_t i = 0; i < 5; ++i)
std::cin >> words[i];
std::cout << std::endl;
for (size_t i = 0; i < 4; i++)
{
bool contr = true;
for (size_t j = 0; j < 5; j++)
{
if(words[i] == words[j])
{
contr = false;
break;
}
}
if(contr == true)
std::cout << words[i] << '\n';
}
std::cin.ignore(2);
return 0;
}
And enter:
one
two
one
four
five
I get no output at all, while the idea is to have:
one
two
four
five
Okay. Here this goes.
What Mr. Lerner tried to explain is that if you start from the start of the array, you would have a hard time keeping track of duplicates.
Ah, ok, let me see whether I can find a solution that way.
I won't let you give up so easily ;)
Thanks for sticking in s.o.s :) Programming is sometimes so frustrating, but, I can't seem to let it go, seems like Ive been bitten by the microbe, problem is, my brains don't seem to be willing to help out here. Glad that this is merely a hobby of mine and I don't have to earn a living with it, wouldn't be earning alot with it :cheesy:
Hope it helped, repost if necessary.
I most certainly will s.o.s, but, it'll probably will be next week since during the week, I don't have time, full time job and other responcebilities.
Hint: wouldn't it be a lot easier if you sorted the current words into alphabetical order first.
Probably, but the exercise goes as follows:
- Read a sequence of words from input. Use Quit as a word that therminates input. Print the words in the order they were entered. Don't print a word twice. Modify the program to sort the words before printing them.
So,
1) Read a sequence of words from input.
2) Use Quit as a word that terminates input.
3) Print the words in the order they were entered.
4) Don't print a word twice.
And
5) Modify the program to sort the words before printing them.
I could try what you suggest, but, then I won't be doing it in the order the exercise says, probably for the reason that N°4 would become easier:?:
Anyway, thanks for the tip iamthwee
Hi Walt,
Well, Lerner posted this helpfull piece of pseudo code:
bool found = false
for each word in the array //this word
for each word prior to this word //current word
if this word is the same as current word
set found to true
if not found
print out this word
And I thought, oh, now I get it:
-First I have a loop for the whole array.
-Then, I have a loop for the word that is being checked whether it exists allready.
-Then, if that word is the same one of the allready added words in the first loop, put the bool variable to true, if not, at the end of looping threw the second loop. Output the word, since it hasn't been added yet.
The idea of the exercise now is to print every word only once, meaning:
one
two
one
four
five
would have to print:
one
two
four
five
Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
Thanks for the kind words s.o.s, but, I can't stop untill I know the answer, so if someone would please help me out with this, Ive been trying and I can't find the solution.
The code Ive got is this:
#include <iostream>
#include <cstring>
int main()
{
char wordArr[5][10];
bool contr = false;
size_t i, j;
for(i = 0; i < 5; i++)
std::cin >> wordArr[i];
for(i = 0; i < 5; i++)
{
for(j = 0; j < i; j++)
{
contr = strcmp(wordArr[i], wordArr[j]);
if (contr == true)
contr = true;
}
if (contr == false)
std::cout << wordArr[i] << '\n';
}
std::cin.ignore(2);
return 0;
}
But, if I use the input:
one
two
one
four
five
The output is:
one
What am I doing wrong :confused:
Getting so frustrated by not finding this solution :mad: And it is probably something simple that I can't see !!!!!!
I GIVE UP !!!!!
Ive been trying to program for over two years now, have been reading several books, did all exercises, but for some unknown reason, I'm not getting it.
So, after some thought about it, Ive decided to stop, I want to thank all of you who have helped me the past two years here on Daniweb and wish you all the best.
Kind regards,
Johan aka JoBe
Why ain't you using std::strings?
>> First post "I know I could use strings or even a vector, but, my idea is to work out a version using characters first, then strings and then a vector." ;)
Just wanted to tell you that sometimes your description is rather vague. Always post a question in such way that the person reading it doesn't have to use a compiler to test it out what is going wrong.
I'll try my best to explain it more thoroughly next time :!:
@ Lerner, thanks for the explanation.
Hi guys,
Was wondering if any of you could help me out, I'm trying to print the words only once, but, it seems I'm doing something wrong, could someone point me in the right direction?
#include <iostream>
#include <cstring>
int main()
{
char wordArr[5][10];
short contr;
for(size_t i = 0; i < 5; i++)
std::cin >> wordArr[i];
for(size_t i = 0; i < 5; i++)
{
contr = strcmp(wordArr[i], wordArr[i+1]);
if (contr != 0)
std::cout << wordArr[i] << '\n';
}
std::cin.ignore(2);
return 0;
}
Hey Lerner,
Finally got time to work on this exercise again, but, before I do, I'd like to ask whether this way of adding a word into another array is good or not. If not, what are the reasons this is not ideal?
for(size_t i = 0; i < 2; i++)
{
size_t j;
std::cin >> word;
for(j = 0; j < strlen(word); j++)
wordArr[i][j] = word[j];
wordArr[i][j] = '\0';
}
The intention here was just two add two words into the array to try out. Was I correct that I had to add the '\0'
to the end of the word so that it would be correctly terminated?
Thanks for those examples Lerner. The reason I came up with my first example code was because the exercise tells you not to store two identical words, therefore I used two variables char word[10]= "";
and char wordArr[10][10];
. That way, I can compare when entering a new word with the already added words to check whether it's not in the list.
I should've written the exercise out, but, I wanted to take this one in small parts as you guys always tell forumers to do, so they don't get overwhelmed with the code. Sorry for that. Anyway, the exercise goes as follows:
- Read a sequence of words from input. Use Quit as a word that therminates input. Print the words in the order they were entered. Don't print a word twice. Modify the program to sort the words before printing them.
I'll be back next week-end, don't have the time to do the exercise now, anyway, thanks again for the thourough explanatioin and examples :!:
Well, found a solution like this:
#include <iostream>
int main()
{
char word[10]= "";
char wordArr[10][10];
for(size_t i = 0; i < 2; i++)
{
size_t j;
std::cin >> word;
for(j = 0; j < strlen(word); j++)
wordArr[i][j] = word[j];
wordArr[i][j] = '\0';
}
for(size_t i = 0; i < 2; i++)
{
for(size_t j = 0; j < strlen(wordArr[i]); j++)
std::cout << wordArr[i][j];
std::cout << '\n';
}
std::cin.ignore(2);
return 0;
}
But, there has to be a way of using char *wordArr[10];
right?
It would make it easier since the use of a pointer to an array of char's wouldn't need to be terminated by yourself, the compiler would do this like Ancient Dragon said in another thread right.
Does any of you guys know how I could use that type of definition in my program?
Hello ladies and gents,
I'm trying to copy two words, for instance test and word into an array of characters and have this:
#include <iostream>
int main()
{
char word[10]= "";
char *wordArr[10];
for(size_t i = 0; i < 2; i++)
{
std::cin >> word;
*(wordArr + i) = word;
}
for(size_t i = 0; i < 2; i++)
std::cout << wordArr[i] << '\n';
std::cin.ignore(2);
return 0;
}
Thing is, it prints the last word twice as if it doesn't store the first word, what am I doing wrong, can anyone point me in the right direction?
I know I could use strings or even a vector, but, my idea is to work out a version using characters first, then strings and then a vector.
...because Jobe has I think got a VC6.
Hi gentlemen,
@s.o.s,
Ive got VS EE free version.
@ Ancient Dragon,
Thanks AD, I thought there ought to be a cleaner solution then (std::string *str = myString; str != &myString[12]; str++)
Also tried to use the vector AD:
#include <iostream>
#include <string>
#include <vector>
void print(const std::vector<std::string>);
int main()
{
std::vector<std::string> myString;
myString.push_back("January");
myString.push_back("February");
myString.push_back("March");
myString.push_back("April");
myString.push_back("May");
myString.push_back("June");
myString.push_back("July");
myString.push_back("August");
myString.push_back("September");
myString.push_back("October");
myString.push_back("November");
myString.push_back("December");
print(myString);
std::cin.get();
return 0;
}
void print(const std::vector<std::string> myArray)
{
std::vector<std::string>::const_iterator itr;
for (itr = myArray.begin(); itr != myArray.end(); itr++)
std::cout << *itr << '\n';
}
Anyway, thank you both gentlemen for the great help :!:
Well, thanks for that, guess I'll have to find out how to change those IDE settings in the Project -> properties -> Configuration properties -> General -> Output Directory?
If you don't mind Salem, could you help me out with this exercise aswell.
It goes like this:
- Find an example where it would make sense to use a name in its own initializer.
I'm not sure what to make out of this, is this related to something like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString = "String";
cin.get();
return 0;
}
Really don't understand what the point of this exercise is ?
Well, Ive checked and checked again, the code still gives the same output, it compiles correctly, no errors nor warnings but once executed, it just keeps on going. If I use str != &myString[12]
however, it works like a charm.
Guess it's a mistery that won't be resolved. Anyway, thanks for your help guys :!:
Well s.o.s, using this code:
#include <iostream>
#include <string>
int main()
{
std::string myArray[] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
for(const std::string* str = myArray ; str != 0; str++ )
std::cout << *str << '\n';
std::cout << '\n';
std::cin.get();
return 0;
}
It compiles without errors or warnings, but once executed, it goes on and on, doesn't seem to find the end of the array.
@ AD,
Thanks AD, but, I thought that begin and end also worked with strings no?
@ s.o.s,
That doesn't work s.o.s, I think it's because strings are not \0 terminated.
Got an additional question gentlemen, there's an exercise that asks the following:
- Find an example where it would make sense to use a name in its own initializer.
I quiete frankly don't understand what is asked here? Do you guys know?
Well, I found a solution, but I'm not sure this is good, to find the starting point was simple, but, I find to use the end of the array of strings not quiet right. Here's how I did it:
for (std::string *str = myString; str != &myString[12]; str++)
std::cout << *str << '\n';
std::cout << '\n';
Hi,
Got another question concerning an array of strings:
I did it this way:
int main()
{
const std::string myArray[] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
for (size_t i = 0; i < 12; i++)
std::cout << myArray[i] << '\n';
std::cout << '\n';
std::cin.get();
return 0;
}
but I thought that there was also a way that you could use the beginning and end of the array to loop threw the array like this myArray.begin() and myArray.end()
doing something like this:
std::string::iterator itr;
for (itr = myArray.begin(); itr != myArray.end(); itr++)
std::cout << *itr << '\n';
std::cout << '\n';
Problem I have is that I'm using an array of strings, I therefore used the following myArray[0].begin() and myArray[11].end()
but that doesn't seem to work.
Could anyone help me out, thank you.
Hi guys,
Well, the next exercise goes as follows:
- Run some tests to see if your compiler really generates equivalent code for iteration using pointers and iteration using indexing. If different degrees of optimization can be requested, see if and how that affects the quality of the generated code.
What Ive got is this:
#include <iostream>
int main()
{
char mySentence[] = {"Hello World!"};
for(size_t i = 0; mySentence[i] != 0; i++)
std::cout << mySentence[i];
std::cout << '\n';
for(char *p = mySentence; *p != 0; p++)
std::cout << *p;
std::cout << '\n';
std::cin.get();
return 0;
}
I don't have a problem with generating the code, what I have a problem with is how to check whether they both generate equivalent code?
- I read that both should generate identical code using a modern compiler and that programmers choose between these two for aesthetical or logical grounds, just how can I check this to be true concerning the identical code?
- Concerning this part of the question: "If different degrees of optimization can be requested, see if and how that affects the quality of the generated code." How do I do that ?
I'm using VCEE and my OS is WinXP Pro if you need to know.
Thanks.
OH, thanks for that Salem, I really thought that the [12] WAS for the amount of months, DARN :confused:
I understand why the way of using
char *month[]
is not only easier, but much faster aswell.
Thanks for the tip s.o.s :!:
Hi,
First use [ code ] code tags [ /code ]. It' makes it alot easier to read code.
Second, show your complet code, where is num declared and how, is it declared as an array?
Third, where is temp declared, it should be declared inside the loop and be a local variable inside the loop.
Hello ladies and gents,
I just made the next exercise of this book and although it works as it should, I was wondering if any of you guys could tell me whether or not it could be improved.
The exercise goes as follows:
- Define a table of the names of months of the year and the number of days in each month. Write out that table. Do this twice: once using an array of char for the names and an array for the number fo days and once using an array of structures, with each structure holding the name of a month and the number of days in it.
I also added the following, initialized the struct with the values of the two arrays days and months and see the sizeof both arrays and the size of the struct and struct array.
Here's the code:
#include <iostream>
#include <string>
struct Date
{
std::string strMonth;
int strDays;
};
int main()
{
Date dt[12];
std::cout << "Using two arrays:\n";
char month[][12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (size_t i = 0; i < 12; i++)
std::cout << days[i] << ' ' << month[i] << '\n';
std::cout << std::endl;
std::cout << "Using an array of struct:\n";
for (size_t i = 0; i < 12; i++)
{
dt[i].strMonth = month[i];
dt[i].strDays = …
Err...I hope you know that he is not exactly a 12 year kid . :mrgreen:
Yeah, figured that out once I saw the way he killed of those void main'ers :cheesy:
Thanks for the correction and those links Salem, I always wonder how you guys come up with such good links you know :)
Hello ladies and gents,
Was wondering if any of you could check this exercise out, I finished it, but, I'm not sure whether Ive got it exactly right, especially the last two definitions.
The exercise is as follows:
- Use typedef to define the types unsigned char, const unsigned char, pointer to integer, pointer to pointer to char, pointer to array of char, array of 7 pointers to int, pointer to an array of 7 pointers to int, and finally, array of 8 arrays of 7 pointers to int.
This is what I have:
include <iostream>
int main() //Use typedef to define the following types
{
typedef unsigned char UChar; //unsigned char
UChar ch = 'a';
std::cout << ch << "\n\n";
typedef const unsigned char CUChar; //const unsigned char
CUChar cch = 'b';
std::cout << cch << "\n\n";
int myInt = 0;
typedef int *pINT; //pointer to integer
pINT pI = &myInt;
std::cout << &myInt << " == " << pI << "\n\n";
typedef char **ppCHAR; //pointer to pointer to char
typedef char *mychArr[]; //pointer to array of char
typedef int *apINT[7]; //array of 7 pointers to int
typedef int **ppMYARRAY[7]; //pointer to an array of 7 pointers to int
typedef int *pMYARRARR[8][7]; //array of 8 arrays of 7 pointers to int
std::cin.get();
return 0;
}
<< // Again, how do I initialize b?
bool b = TRUE;
if (*fp == *(fp+1))
return true;
else
return false;
Hehe, thanks AD, think I got it now ;)
Hey thanks for those links s.o.s, I'll read them tomorrow, I tried to find something online about it, but never seem to use the correct words to find the good links :cheesy:
Thanks again.
Pointer of type void can point to any type of data if thats what you asking while int type pointers point only to int data and char type pointers point to char type data.
I know about that, but thanks anyway.
Microsoft compilers has an alignment option to align data on 1, 2, 4, 8, or 16 byte boundries. I think Dev-C++ and other compilers do that too. This is to help improve memory access time.
Hi AD,
But, so how can I find out what my restrictions are to pointer types char*, int*, and void* ?
The three have a size of 4Bytes, no matter what type it is. But, how to find out the restrictions?
Hi s.o.s,
Thanks for the reply, I know about the multiples of 4, because if you take the code below here, the sizeof(address) should give 22 and gives 24 as result. It's written that if you want to minimize wasted space you should simply order the members(from large to small). I tried this in the below written code but to no avail, sizeof(address) still remains 24.
#include <iostream>
struct address
{
char *name;
char *street;
char *town;
long int number;
long zip;
char state[2];
};
int main()
{
std::cout << sizeof(char*) << '\n';
std::cout << sizeof(char*) << '\n';
std::cout << sizeof(char*) << '\n';
std::cout << sizeof(long int) << '\n';
std::cout << sizeof(long) << '\n';
std::cout << sizeof(char[2]) << '\n';
std::cout << sizeof(address) << '\n';
std::cin.get();
return 0;
}
Thing is, the part for instance about "For example, may an int* have an odd value? Hint: alignement."
What a question is that, a pointer to an int points to an address, aslong as the value in that pointer which is pointed to is of an integer value, it doesn't matter what value it has right?
Really confusing question, "restrictions on pointer types char*, int*, and void* ?" :confused:
Hello ladies and gents,
I'm reading The C++ PL from B.Stroustrup and in chapter 5 the second exercise goes like this:
What on your system, are the restrictions on the pointer types char*, int*, and void*? For example, may an int* have an odd value? Hint: alignement.
I actually have no idea what is asked here, I was thinking that it was related towards what the max values of the different pointer types can hold as value?
Could someone explain what is actually requested?
Thank you.
<<"In reference to the anti-virus consideration-- that sounds unusual-- I did not try it, but I appreciate the info."
>>I know it sounds unusual, but, it did solve my problem. ;)
By the way, thanks for that link Wolfpack, seems I didn't use the proper search words for that kind of tutorial :)
#include <iostream> #include <iomanip> #include <string> using namespace std; // Function Prototypes float division_input(string); void highest_division(float, float, float, float); int main() { ... } // *********Subroutine definitions below********** float division_input { ... }
Look at the red part, your function has four parameters, but you don't use them when you call your function.
System 'pause' should be avoided due to incompatibility.
Hello ladies and gents,
I know this is an old thread, but, just in case anyone else has similar problems, I found the cause of the problem.
Up untill now, I didn't find a solution in to getting the debugger to work correctly, but I started to look for a solution again and changed a few parameters in the configuration panel which I thought where necessary to get it to work, but again, to no avail.
However, I highered my Panda virus protection to give me an indication of any virus, hack, etc... that was trying to intrude my computer and what did I see, my anti virus system was saying that my .exe that I was working with on that moment was a virus. To test it out, I disconnected my antivirus system for a minute and tried to use the debugger again and it worked the way it was supposed to work.
Maybe this is hard to believe but, it's working now, I only have to figure out how I can keep my antivirus system working and use the debugger at the same time.
Everything is working now, closed anti-virus, opened it again, and the debugger still works, closed VS EE and opened it again, opened the program and it still is working, keeping fingers crossed. :)
Show us what you have so far, it's much easier that way to help you!
Oh, I get it now, this means that when I or anyone else use <windows.h> it wouldn't be portable to any other type of OS.
Then, what would you use in replacement of <windows.h> so you could write code and use it on different OS?
It wasn't about being bothersome AD, I was just curious as to why it still had an .h extension while other headers in C++ don't use .h extensions anymore.
Thanks for the explanation guys.
Thanks Salem, but if it doesn't belong to either standard, too what does it belong then?
Hi ladies and gents,
Wanted to ask a simple question probably for you guys and the answer will maybe be simple aswell, but, I thought, what the heck, I'll go for it :)
It just seems strange that when everyone tells to use <iostream> instead of <iostream.h>, <cstdlib> instead of <stdlib.h>, etc... that this is not the case for <windows.h> ?
Is there a particular reason for this?
I know it's not really important, but, I really am curious as to know what is the reason for not changing this.
Thanks.
Well, I found a solution to get the original settings once again --> Tools --> Import and Export Settings ...
So, I tried once again to set a breakpoint at the function call and press F11 to see whether I would be brought to the function, but alas, once again, the ostream headerfile opened once again.
If you or anyone else is reading this Wolfpack, could you please see which settings I have to change to get the correct result :)
Thanks.
If you decide to do that REMEMBER TO BACKUP YOUR REGISTRY BEFOREHAND.
Good luck. Tell me how it goes.
Well, thanks for the help, but, I prefer to stay clear of that, think the only thing left is to keep on trying and change some parameters in the Property Settings.
Sorry for the delay. I was cooking. :cheesy:
No problem, enjoy your meal ;)
I ran your code on a Visual C++ Express that I installed just yesterday. It is still on the factory settings.
That's what I'm not certain of anymore, I think I allready changed some settings, don't remember which ones exactly :o
When I set the breakpoint in line number 19,
for (int i = 1; i <= 9; ++i){ multi(i);// this is line number 19 }
Pressing F11 will take you to the multi function. However if you press F11 where there is a line with cout, you will be taken to the ostream file. Maybe that is where you are doing wrong. For those places try F10.
There are some problems with those:
1) When I'm even on
cout << i << ' ';
this line and press F10, it just stays there and doesn't do anything. When pressing F11 I get the ostream headerfile. The only way to get out of here is when I use Step Out.
2) When I used Step Out and get towards the code line N°19 as you say, press F11, I get back towards ostream headerfile. When using F10, it just stays in place and doesn't do anything.
Hi Wolfpack,
No it doesn't, when I put a breakpoint at a certain function call like:
multi(i); in main
and the debugger gets to that point, and I press Get Into, I am being brought to the ostream header, and not into my function which I would like to follow.
Example:
#include <iostream>
using namespace std;
void multi(int& x)
{
cout << x * 2 << ' ';
}
int main()
{
//insert elements from 1 to 9
for (int i = 1; i <= 9; ++i){
cout << i << ' ';
}
cout << endl;
for (int i = 1; i <= 9; ++i){
multi(i);
}
cout << endl;
cout << "Press enter to exit." << endl;
cin.get();
}
Hello ladies and gents,
Wanted to ask if someone could help me out with the debug settings in VC++ 2005.
What I want to be able to do is to follow my program step by step and see what happens with the variables.
I know it's related to the settings in the Project Properties, but, problem is, I don't know what to change in the C/C++ and Linker tabs.
I have changed for example the C/C++ -> General -> "Debug Information Forma" -> into -> Program Database for Edit & Continue (/ZI). But, I have to change these in the linker tab aswell, but, don't know which to change into what???
I was hoping any of you could help me out here.
Are you sure? it works with me fine...
i am still looking for selection and insertion code...
Euhm, didn't look really well now did you :cheesy:
Eternally Confuzzled's Insertion sort
Hi Narue,
Yep, you understood it perfectly, thanks for the help ;)