357mag
Members-
Content Count
104 -
Joined
-
Last visited
Community Reputation
3 NeutralRecent Profile Visitors
The recent visitors block is disabled and is not being shown to other users.
-
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 << '.'; }
-
Excuse me sir for asking a question. Jump in a lake.
-
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?
-
Okay I got it going now.
-
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; }
-
How do I use a range-based for loop to sum all elements
357mag replied to 357mag's topic in General Help
Yes that works great. Thank You. -
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;
-
How do I use a range-based for loop to sum all elements
357mag replied to 357mag's topic in General Help
Something is still off. My console window is saying that sum is equal to 1985788. -
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; }
-
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?
-
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.
-
Someone asked to see the code. So I post the code.
-
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; }
-
There is no code that behaves differently.
-
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.