Trying to decide which programming language I want to learn

  • Thread starter yungman
  • Start date
  • #326
33,945
5,595
I yet to find ways not to manipulate data better, how to NOT erase the old data if I choose to and add new data to the end of the stream
To do this, you need to open the file in append mode. Here's an example that I modified from the VS documentation (ios_base class, https://docs.microsoft.com/en-us/cpp/standard-library/ios-base-class?view=vs-2019#ios_base):
C++:
// ios_base_openmode.cpp
#include <iostream>
#include <fstream>
using std::fstream;
using std::ios_base;
int main()
{
    fstream file;
    file.open("rm.txt", ios_base::app);
    file << "testing\n";
}
Here I'm opening the file in append mode, which implies that the file is an output file. There are several other flag values, including in, out, trunc, ate, and binary.
how to choose to read data instead of starting from the first piece every time.
If you want to open a file for reading, but want to start somewhere other than the beginning of the file, you need to move the position-in-file pointer to where you want it, and then start reading from there. To do this, you can use basic_istream::seekg(). There's a so-so example here -- https://docs.microsoft.com/en-us/cpp/standard-library/basic-istream-class?view=vs-2019#seekg. There is another example here: http://cplusplus.com/reference/istream/basic_istream/seekg/.
 
  • Like
Likes yungman
  • #327
4,738
102
To do this, you need to open the file in append mode. Here's an example that I modified from the VS documentation (ios_base class, https://docs.microsoft.com/en-us/cpp/standard-library/ios-base-class?view=vs-2019#ios_base):
C++:
// ios_base_openmode.cpp
#include <iostream>
#include <fstream>
using std::fstream;
using std::ios_base;
int main()
{
    fstream file;
    file.open("rm.txt", ios_base::app);
    file << "testing\n";
}
Here I'm opening the file in append mode, which implies that the file is an output file. There are several other flag values, including in, out, trunc, ate, and binary.
If you want to open a file for reading, but want to start somewhere other than the beginning of the file, you need to move the position-in-file pointer to where you want it, and then start reading from there. To do this, you can use basic_istream::seekg(). There's a so-so example here -- https://docs.microsoft.com/en-us/cpp/standard-library/basic-istream-class?view=vs-2019#seekg. There is another example here: http://cplusplus.com/reference/istream/basic_istream/seekg/.
Thanks, I'll try this later.
 
  • #328
4,738
102
This is the program I wrote that is away from the examples in the book that are very simple. I actually drew the flow chart first to write the code. I attached the flow chart.

The program is to keep temperature of oven between 101deg C to 103deg C. It check the temperature, if it is too low, turn up the over, wait for 5 mins and recheck, repeat until the temperature is equal or higher than 101deg C. If temperature is too high, turn down the over and wait for 5 minutes and read the temperature again, repeat until temperature is below or equal to 103deg C. If the temperature is within range, check every 15 minutes. The program will ask whether you want to quite, if not, it will keep checking. This involve switch-case and do-while.

C++:
//this program check temp, if 101 <=temp<=103deg C, check every 15mins.
//If it's over, turn it down, check every 5 minutes until correct temp.
#include <iostream>
using namespace std;

int main()
{
    int temp = 100;
    char userSelection = 'A';
    char caseSelect = 'C';
    const char ENT = '\n';
  
    do
    {
        cout << "What is the temperature?   ";
        cin >> temp; cout << endl; cout << endl;//Read temperature.

        if (temp < 101) caseSelect = 'A';//Temperature too low.
        else if (temp > 103) caseSelect = 'B';//Temperature to high.
        else caseSelect = 'C';// Temperature in range.

        switch (caseSelect)
        {
        case ('A'):// Temperature too low.
            {while (temp<101)
                {
                cout << "Raise the temperature, wait 5 minutes and measure again.\n\n";
                cout << "Read temperature.  "; cin >> temp;
                }
            }
      
        case ('B')://Temperature too high
            {while (temp>103)
                {
                cout << "Lower the temperature, wait 5 minutes and measure again.\n\n";
                cout << "Read temperature.  "; cin >> temp;
                }
            }
      
        case('C')://Temperature in range.
            {
            cout << "Wait 15 minutes and measure again.\n\n";
            }
          
            cout << "Hit ENTER if you want to quit, hit any other key to continue.\n\n";
            cin.ignore(255, '\n');// Clear '\n' from last ENTER.
            cin.get(userSelection); cout << endl; cout << endl;//Read whether to quit.
        }
    } while (userSelection != ENT);
    cout << "you chose to quit, goodbye.\n\n";
    return 0;
}
It is working, I don't have any question. Just want to show my first program I wrote. Thanks for all the help so far.
 

Attachments

  • #329
jtbell
Mentor
15,613
3,636
Just curious... why did you do it in two steps via the caseselect variable? Simply to get some practice with the switch statement?
 
  • Like
Likes yungman
  • #330
4,738
102
Just curious... why did you do it in two steps via the caseselect variable? Simply to get some practice with the switch statement?
Thanks for taking the time to look at it.

This is not a practice on switch-case, I actually want to write a more complicate program than what the book shows. It's not even started out using switch-case. I want the program to be as simple and straight forward on the flow. I believe my design is the simplest, no crossover from one path to the other, the flow going straight down.

Also, I take care of what to me the most important thing, that is to take care of under damp situation like if the temperature is too high, I lower the heat, it can get too low under 101degC, I want a way to recover that by raising the oven temperature. This program will do that so it will make sure it will get to the temperature range between 101 and 103deg C.

Yes, I found out after design the program, that switch-case doesn't take conditional statement. That is switch can only take simple case of A, B, C, D etc. It will not read

switch ( temp)
case ( temp <101):
case (temp >103):

I have no choice but to use if-else if - else to put A, B and C for the case statement.

This program is really like the kindergarten version of closed loop feedback temperature control. I can see in the future, I can add in time constant and oven control. Like if the temperature is over 10deg below the optimal range, I would turn the oven full blast until it's within say 5deg. Then I back off the oven and check more frequently. Then if it is within 2deg, I back off even more to let the temperature ease into the optimal range.

thanks
 
Last edited:
  • #331
33,945
5,595
Yes, I found out after design the program, that switch-case doesn't take conditional statement. That is switch can only take simple case of A, B, C, D etc. It will not read

switch ( temp)
case ( temp <101):
case (temp >103):

I have no choice but to use if-else if - else to put A, B and C for the case statement.
You can make the program a lot simpler by eliminating the switch statement. Also, it simplifies things greatly if you don't confuse the user by asking for numeric and character input (i.e., asking for a temperature followed by the Enter character to quit).

Here's my simplified version.
C++:
// Program checks temperature.
// If 101 <= temp <= 103deg C, check every 15mins.
// If temp > 103 C, turn heat down, check every 5 minutes until correct temp.
// If temp < 101 C, turn heat up, check every 5 minutes until correct temp.
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int temp;

    do
    {
        cout << "Enter the temperature, or a negative number to quit.   ";
        cin >> temp;            // Read temperature.
        if (temp < 0) break;    // If negative, exit do loop.
       
        if (temp < 101)
        {
            // Temperature too low.
            cout << "Raise the temperature, wait 5 minutes and measure again.\n\n";
            continue;

        }
        else if (temp > 103)
        {
            // Temperature too high.
            cout << "Lower the temperature, wait 5 minutes and measure again.\n\n";
            continue;
        }
        else
        {
            // Temperature is between 101 and 103 degrees C.
            cout << "Wait 15 minutes and measure again.\n\n";
            continue;
        }               

    } while (true);
    cout << "You chose to quit, goodbye.";
}
The do ... while loop is an infinite loop that uses continue to start a new loop iteration, and break to get completely out of the loop.

Input from the user is asked for once, at the top of the loop. The user can enter any integer value 0 or larger, or can enter any negative number to exit the program.
 
  • Like
Likes yungman
  • #332
4,738
102
You can make the program a lot simpler by eliminating the switch statement. Also, it simplifies things greatly if you don't confuse the user by asking for numeric and character input (i.e., asking for a temperature followed by the Enter character to quit).

Here's my simplified version.
C++:
// Program checks temperature.
// If 101 <= temp <= 103deg C, check every 15mins.
// If temp > 103 C, turn heat down, check every 5 minutes until correct temp.
// If temp < 101 C, turn heat up, check every 5 minutes until correct temp.
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int temp;

    do
    {
        cout << "Enter the temperature, or a negative number to quit.   ";
        cin >> temp;            // Read temperature.
        if (temp < 0) break;    // If negative, exit do loop.
    
        if (temp < 101)
        {
            // Temperature too low.
            cout << "Raise the temperature, wait 5 minutes and measure again.\n\n";
            continue;

        }
        else if (temp > 103)
        {
            // Temperature too high.
            cout << "Lower the temperature, wait 5 minutes and measure again.\n\n";
            continue;
        }
        else
        {
            // Temperature is between 101 and 103 degrees C.
            cout << "Wait 15 minutes and measure again.\n\n";
            continue;
        }            

    } while (true);
    cout << "You chose to quit, goodbye.";
}
The do ... while loop is an infinite loop that uses continue to start a new loop iteration, and break to get completely out of the loop.

Input from the user is asked for once, at the top of the loop. The user can enter any integer value 0 or larger, or can enter any negative number to exit the program.
Thanks for taking the time. I did started out with flowing through concept like what you have. But my goal is to continuous monitoring. It became messy doing it this way.

Like I explain before, my program is like a kindergarten version of closed loop feedback temperature control, not just doing one adjustment and it's done. The way I loop is to monitor over and under shoot of temperature and adjust so it will settle into the optimal range. I can envision in the future, I can add time constant within the do-while that nested under the switch-case to control the oven, if the Temp is over 10deg below the lower limit, crank the oven high, but when temp approach the lower limit, turn the oven down and ease into the optimal range. Then when it's in optimal range, keep it there by touching up the oven control every time it loops around.

I did this more in system point of view, I spent a few hours designing the flow chart, only like half an hour to code it.

I can't wait to get to chapter 6 functions and chapter 9 pointers.

Thanks for you time to look at it.
 
Last edited:
  • #333
4,738
102
I am studying chapter 6 Functions. I am confused, I read all along that C++ is an Object Oriented programming, that the program calls on Objects to perform a certain task. That an Object is a self contained unit the consists of it's data and codes to work on the data. That the program call on the Object with given parameters and the Object performs the task and return back the parameters.

But this is EXACTLY what Gaddis book described as Function!!! That the main() is a function, each of the Object ( subroutine) it calls is a Function. That the Headers the program used in #include <> contain all the Function needed.

What am I missing? This goes right back to my biggest road block in learning the C++ again. I have to say after writing quite a bit of programs by now, coding and even designing the program is the EASY part of C++. This kind of fancy naming is the difficult part and the most confusing part. I am so lucky I decided to order my grandson's book( Gaddis), or else I would be still be struggling.
 
  • #334
jtbell
Mentor
15,613
3,636
Again, I will say that I don't have Gaddis's book, so I am just guessing. Nevertheless, based on my experience with C++ textbooks many years ago, I will guess that he and you are still in the realm of procedural programming.

I will guess that a following chapter will introduce you to "structs", which are collections of data of different types, as opposed to arrays, which are collections of data of the same type.

Then, a later chapter will introduce you to "classes". They define objects that are basically structs that also include "member functions" that act on the data that the objects contain. Then you will enter the realm of object-oriented programming.
 
  • Like
Likes yungman
  • #335
Dr Transport
Science Advisor
Gold Member
2,412
541
Gaddis gets to objects and structures in the next chapter. He is working you thru procedural programming then he eases you into object oriented. The best layman's description I can give is that a function can return one thing only, i.e., a matrix, a float etc....whereas a structure with it's defined functions, can return a collection of things.

I remember when I was taking the course, we had to write a program to calculate trhe propertiesd of a rectangle, the area and perimeter. Both take the exact same arguments and I remember saying that if I could return two things from this function, I'd only have to write one function. Whohooo when I learned about structures and classes......
 
  • Like
Likes jtbell and yungman
  • #336
4,738
102
Gaddis gets to objects and structures in the next chapter. He is working you thru procedural programming then he eases you into object oriented. The best layman's description I can give is that a function can return one thing only, i.e., a matrix, a float etc....whereas a structure with it's defined functions, can return a collection of things.

I remember when I was taking the course, we had to write a program to calculate trhe propertiesd of a rectangle, the area and perimeter. Both take the exact same arguments and I remember saying that if I could return two things from this function, I'd only have to write one function. Whohooo when I learned about structures and classes......
I guess I am too impatience!!! I'll wait till I get there.

thanks
 
  • Like
Likes Dr Transport
  • #337
4,738
102
I am back to my question of my original title of this thread. I am going to finish this C++ following the college class. I want to look ahead to what's next to plan ahead. I picked C++ first as it's the closest to my trade, hardware application where C++ is closest to the real hardware and the fastest.

1) What language Windows use in their programs? Like if I want to understand windows, what language should I learn next or what stuff should I learn? I have a suspicion it's NOT C++.

2) What language is most common for web design?

3) What language is most common for writing games?

Thanks
 
  • #338
35
29
2) What language is most common for web design?
Do you mean "web design" as in the graphic design and layout of the page or are you using the term to me "web delvelopment" that is the creation of websites and web based apps.

In terms of web design, it is mostly HTML and CSS, both of which are pretty simple and straight forward. To some extent Javascript (JS). But the use of JS begins to blur the line between design and development.

In terms of web development, you must also make the distinction between server side and client side. For the server side, the most common language is most likely PHP. Client side it is by far JS as it is native to every available browser. Other languages would require some kind of client download.

Personally I don't like PHP, I much prefer Python, you can use frameworks such as Flask, Django or multitude of others. Then you can use the same language to other non-web related things. And, you can then integrate your non-web things with the web and not require any other languages.

Client side, I like to stick with plain Javascript, but you can use other frameworks such as React, Angular, or JQuery and then also use JS server side with NodeJS.
 
  • Like
Likes sysprog and yungman
  • #339
4,738
102
I was searching on google, it said Windows Kernals are written in C, C++ or C#. Does that mean the Windows is written in C related language, that I am learning the right language?
 
  • #340
PeterDonis
Mentor
Insights Author
2019 Award
30,656
9,667
I was searching on google, it said Windows Kernals are written in C, C++ or C#. Does that mean the Windows is written in C related language, that I am learning the right language?
Unless you are planning on writing Windows kernel code, or a Windows driver for some piece of hardware, you don't have to write Windows programs in the same language as the Windows operating system is written in.

AFAIK the language Microsoft currently seems to be pushing for people to write Windows programs in is F#, which is a sort of successor to C#. I personally don't have any need or desire to use anything Microsoft is pushing; when I need to write code to run on Windows, I write it in Python.
 
  • Like
Likes sysprog
  • #341
4,738
102
Unless you are planning on writing Windows kernel code, or a Windows driver for some piece of hardware, you don't have to write Windows programs in the same language as the Windows operating system is written in.

The language Microsoft currently seems to be pushing for people to write Windows programs in is F#.
Thanks for the reply, I am more interested in understanding windows, I thought knowing that language allows me to read some of their codes to understand it better. Understand how the kernels and drivers work is good too.
 
  • #342
1,628
881
Thanks for the reply, I am more interested in understanding windows, I thought knowing that language allows me to read some of their codes to understand it better. Understand how the kernels and drivers work is good too.
Windows is mostly supplied as object code only (non-source), although various versions of various pieces of it have been leaked. MS has provided a lot of other material for open source distribution. MS recently released source code for Windows Calculator
https://github.com/Microsoft/calculator
and for Windows File Manager
https://github.com/Microsoft/winfile/

For general understanding of inner workings of Windows, you might check out Inside OS/2 (1988), by Gordon Letwin.
 
Last edited:
  • Like
Likes yungman
  • #343
4,738
102
Thanks for the reply, sounds like people can use any program to write to interface with Windows. I kept thinking you have to match the language of the program written in order to interface and work with the existing program. I guess you don't have to, you just need to know the interface.

Is it generally true that you can use C++ to interface with any other language? Is there a standard of interfacing that people follow?

If that is all true, then there is NO need to learn different languages, you just need one and you can do everything!!! I understand that maybe it's more convenient using one language over another for certain situation, but it can be done using one language only. Is that true?

BTW, I am surprised I don't have question lately. With Gaddis, it's been smooth. Particular as I learn more, when I google, I found out they are not all written in Russia!!! Some actually written in English!!! At the beginning, when you guys kept saying google first, but when I did that, they all seemed to be written in Russian to me!!! Now it's getting better.

Thanks
 
  • #344
PeterDonis
Mentor
Insights Author
2019 Award
30,656
9,667
I kept thinking you have to match the language of the program written in order to interface and work with the existing program. I guess you don't have to, you just need to know the interface.
Windows is not a program, it's an operating system. All operating systems have interfaces that any program has to use, and all programming languages have library functions that know about the interfaces of all operating systems that those programming languages can run on.

Programs in general don't have interfaces; they aren't designed to have other programs work with them. They're just designed to do whatever it is they do.

Is it generally true that you can use C++ to interface with any other language?
No.

Is there a standard of interfacing that people follow?
No.

If that is all true, then there is NO need to learn different languages, you just need one and you can do everything!!! I understand that maybe it's more convenient using one language over another for certain situation, but it can be done using one language only. Is that true?
You can in principle write pretty much any program in any language. But many programs are much easier to write in some languages than in others.
 
  • Informative
Likes yungman
  • #345
4,738
102
...................
You can in principle write pretty much any program in any language. But many programs are much easier to write in some languages than in others.
What is the best language to interface with Windows?

What is the most popular language to write pc gaming?

Thanks
 
  • #346
4,738
102
I spoke too soon. I fail to compile this program. It's a very simple program but if failed. It is EXACTLY as in the book, I checked many times and I also understand what the program want and everything is correct.
C++:
//Default Arguments
#include <iostream>
using namespace std;

void displayStars(int = 10, int = 1);// Default argument to 10 and 1 in prototype.

int main()
{
    displayStars(); cout << endl;
    displayStars(5); cout << endl;
    displayStars(7,3); cout << endl;
    return 0;
}

void displayStars(int cols, int rows)
{
    for(int down = 0; down < rows; down++)
    {
        for (int across = 0; across < cols; across++)
        {
            cout << "*";
        }
        cout << endl;
    }
}
The error message is:
Compile error file write.jpg


Can anyone see what's happened? What does that mean of cannot open the file? It's opened, I am working on it!!

Thanks
 
  • #347
1,628
881
Next time you get an error message, please try looking it up:

1597002465771.png

You can see from the explanation that the error has nothing to do with the program code.
 
  • #348
PeterDonis
Mentor
Insights Author
2019 Award
30,656
9,667
What is the best language to interface with Windows?
I don't think there is a single "best" language to interface with any operating system. All major languages know how to interface with all major operating systems.
 
  • Like
Likes sysprog
  • #349
4,738
102
Next time you get an error message, please try looking it up:

View attachment 267544
You can see from the explanation that the error has nothing to do with the program code.
I read a few this before I posted, I don't understand why.

It is strange. I copy the .cpp out, created another project, put in the EXACT source.cpp. It ran on the new project under new name.

What went wrong, that's what I don't understand.

It's even more strange, I closed VS, I try to delete the whole program folder that has problem, I can't even delete it, it said the program is opened somewhere!!! I closed everything. The only other thing left is restart the computer!!!
 
  • #350
1,628
881
I read a few this before I posted, I don't understand why.

It is strange. I copy the .cpp out, created another project, put in the EXACT source.cpp. It ran on the new project under new name.

What went wrong, that's what I don't understand.
The message explanation refers to what amounts to a version control problem (can't find referenced file in this version) that is entirely unrelated to anything in the .cpp code.
 

Related Threads on Trying to decide which programming language I want to learn

Replies
7
Views
4K
  • Last Post
Replies
14
Views
3K
  • Last Post
2
Replies
43
Views
3K
  • Last Post
Replies
9
Views
890
Replies
17
Views
2K
  • Last Post
2
Replies
29
Views
2K
Replies
9
Views
855
Replies
15
Views
3K
Top