Jump to content

357mag

Members
  • Content Count

    111
  • Joined

  • Last visited

Everything posted by 357mag

  1. 357mag

    I keep being off by one

    It seems I'm always off by one. This program changes the contents of an array using a function and a pointer. The problem is the while loop in the function. At some point the value of num has to become 0 for the loop to fail and stop printing numbers. But on paper when I plot this thing out, I have already printed all 10 squares 1 through 10, but the value in num is still 1. Shouldn't at that point the value of num be 0? But I keep getting 1. So it's like all 10 squares have been printed but the condition in my while loop looks like this: while(1) instead of... while(0) Here is the code. Sorry I have not changed the namespace stuff yet: void square(int *n, int num); int main() { int i, nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout << "Original array values: "; for(i = 0; i < 10; i++) cout << nums[i] << " "; cout << endl; square(nums, 10); cout << "Altered array values: "; for(i = 0; i < 10; i++) cout << nums[i] << " "; cout << endl << endl; system("pause"); return 0; } void square(int *n, int num) { while(num) { *n = *n * *n; num--; n++; } }
  2. 357mag

    I keep being off by one

    I have been able to successfully step into or trace into but I can't recall how I did it.
  3. 357mag

    I keep being off by one

    Same thing happens. If I put the breakpoint next to while(num) and hit F7, the same window with all those crazy addressess shows up. If I put the breakpoint next to the function call square(nums) I get this screen:
  4. 357mag

    I keep being off by one

    I put my breakpoint on the function header: void square... ...and when I pressed Trace Into I got all that crazy stuff that looked like addresses.
  5. 357mag

    I keep being off by one

    I can't use Trace Into because this is what I see when I use it:
  6. 357mag

    I keep being off by one

    Yes num reaches 0. I'm just saying when I put all this stuff on paper, it seems after the last square has been printed, there still is one iteration left to go. So I end up with while(1) instead of while(0). But it's possible that my paperwork was just off a little. I understand how the program works.
  7. 357mag

    I keep being off by one

    Plus I was trying to use the debugger so I could watch the value of num in the while loop but there is no step into command. Just step over and trace into. Actually, I've never used a debugger, so I'm new to that. I'm pretty close on this maybe my paperwork is just a bit off.
  8. Cool program which I like using a global variable. But the compiler says reference to count is ambiguous. It is one of Herb's programs, and I know his style of writing program is suspect, but his programs themselves as far as his ideas I have always liked. Here it is: #include<iostream> using namespace std; void functionOne(); void functionTwo(); int count; int main() { int i; for(i = 0; i < 10; i++) { count = i * 2; // reference to count is ambiguous functionOne(); } system("pause"); return 0; } void functionOne() { cout << "count: " << count; cout << endl; functionTwo(); } void functionTwo() { int count; for(count = 0; count < 3; count++) cout << '.'; }
  9. 357mag

    Pointer arithmetic question

    I'm trying to understand some basic pointer arithmetic. I guess when a pointer is incremented (like ptr++) it does not get incremented by 1. Instead the address increases by 1 multiplied by the size of the data type it is pointing to. If ptr initially held an address called 2000, then ptr++ would make it point to address 2004. What I don't understand is I read "each time ptr gets incremented, it will point to the next integer." What does that mean? The next integer? What next integer?
  10. 357mag

    Pointer arithmetic question

    Excuse me sir for asking a question. Jump in a lake.
  11. I want to print a line of text using cout but there is a word that I want to have quotation marks around it. I want the output to look like this: Looking for the index position of the word "own". What I've tried so far has not worked. Here is what I got currently: int main() { string sentence = "To thine own self be true"; string word = "own"; cout << "The sentence is: " << sentence << endl; cout << "Looking for the index position of the word \"own\"; cout << sentence.find(word) << endl; system("pause"); return 0; }
  12. 357mag

    How to get the quotation marks to show

    Okay I got it going now.
  13. I'm considering working in Delphi for awhile, and I was looking through my books (I have four of them), and every book starts off right away with Windows programming. None of the books teaches you console programming. Personally, I like console programming. I was wondering if there is a reason all of my books starts you off that way. Perhaps because Delphi is a clean, fairly easy to grasp language?
  14. 357mag

    Program works fine but why?

    Just one question about the way this program is working. I make a constant and initialize it to 10. I use that to make the size of the array called name1. What I don't understand though is if I run the program and enter in a long name that is longer than 10 characters, C++ still allows me to do it and the program works just fine. I was expecting an error message saying something about 'out of bounds error". I don't get why C++ is allowing me to enter in a name that is longer than the size of the array: #include<iostream> #include<cstring> using namespace std; int main() { const int SIZE = 10; char name1[SIZE]; char name2[SIZE] = "C++owboy"; cout << "Howdy! I'm " << name2 << "!"; cout << " What's your name?" << endl; cin >> name1; cout << "Well, " << name1 << " your name has "; cout << strlen(name1) << " letters" << endl; cout << "Your initial is " << name1[0] << endl; name2[3] = '\0'; cout << "Here are the first three characters of my name: "; cout << name2 << endl << endl; system("pause"); return 0;
  15. I've never used range-based for loops so I don't know how to write one that is keeping track of the sum of the elements in an array: int main() { int myArray[] = {2, 4, 7, 8, 9}; cout << "The values in the array are: "; for(int n : myArray) cout << n << " "; int sum = 0; for(int x : myArray) { x += myArray[x]; // don't know what to write here } cout << "The sum is: " << x << endl; // use of undeclared identifier system("pause"); return 0; }
  16. Yes that works great. Thank You.
  17. Something is still off. My console window is saying that sum is equal to 1985788.
  18. I'm looking around the IDE for something that will allow me to add a header file to my project. Do I right-click on the project and just select Add Unit? I also need to add a .cpp file too.
  19. 357mag

    How do I add a header file to my project?

    Oh I believe I found it. Anyway I made my first project that uses a header file to hold the function prototype, and a .cpp file that holds the implementation. The int main( ) file I named Odd Or Even.cpp (The program determines whether two numbers are odd or even). The header file I added to the program it holds the function prototype I named it MyLib.h The additional .cpp file I added that holds the implementation I named it MyLib.cpp I'm not too clear on the last file which C++ Builder generates when you create a new project. C++ Builder calls it PCH1.h. Here it is: #ifdef _WIN32 #include <tchar.h> #endif bool isEven(int number); But it seems like I have two function prototypes going on. One here in this PCH1.h file, and the other one in the MyLib.h file. I had to place the function prototype here because the compiler complained about an undeclared identifier without it. Then I commented out the prototype in the MyLib.h file and rebuilt the project and it ran fine so I deleted that file. But what is this PCH1.h file about with this code that automatically comes with it? PCH stands for what?
  20. Here is one of my projects which was written in an earlier version of C++ Builder. You will notice that I'm using #include<conio.h> plus I'm using getch(); // This program demonstrates writing a simple function to cube a number #include <iostream> #include <conio.h> using namespace std; int cube(int y); // function prototype int main() { int x; cout << "Enter an integer value: "; cin >> x; int answer = cube(x); // call the cube function and pass x as an argument and assign result to the variable answer cout << "That number cubed is " << answer << endl; // print the result getch(); return 0; } int cube(int y) // function definition { return y * y * y; }
  21. 357mag

    Earlier version of C++ Builder program

    Someone asked to see the code. So I post the code.
  22. 357mag

    This is a change

    In previous versions of C++ Builder, I remember having to use the include<conio.h> header file. Plus to keep the console window open I had to put getch() at the end of my source file. Now I just copied a .cpp file which was made in Visual Studio, and I pasted it into a source file in C++ Builder. I didn't think it would run, but it did with no complaints from the compiler. I did not include the <conio.h> file nor did I have to place a getch() at the end. C++ builder simply ran the file just fine. Did Embarcadero change some things? I'm using the Community Edition 12.
  23. 357mag

    This is a change

    There is no code that behaves differently.
  24. I'm working on a program using Logical Operators, and everything is working fine, except for one line that the compiler says it's incompatible types. It's a bit messy to look at also, but it works. And is there a way to tell Delphi to print the words true and false instead of the integer values -1 and 0? The line that is not correct yet is the fifth line down (the MemoResult lines) the one where I'm using the not operator. I don't know how best to write it. Here is my code: procedure TFormLogicalOperators.ButtonOKClick(Sender: TObject); var a: boolean; b: boolean; begin a := true; b := false; MemoResult.Text := ('a: ' + BoolToStr(a)) + sLineBreak; MemoResult.Text := MemoResult.Text + ('b: ' + BoolToStr(b)) + sLineBreak; MemoResult.Text := MemoResult.Text + ('a and b: ' + BoolToStr(a and b)) + sLineBreak; MemoResult.Text := MemoResult.Text + ('a or b: ' + BoolToStr(a or b)) + sLineBreak; MemoResult.Text := MemoResult.Text + ('not a: ' + BoolToStr(not (a + b))) + sLineBreak;
  25. 357mag

    One line of code not quite right

    Where do you place the function header? Calling a function that is already defined somewhere is one thing. Declaring or defining it is another. I may have to come back to this program later on.
×