Trying to decide which programming language I want to learn

In summary: C++ is considered a lower-level language, meaning that it is closer to the hardware and gives you more control over how your program runs. However, C# is a high-level language, which means it is easier to learn and use and has a lot of built-in libraries that make coding faster and more efficient. Both languages have their advantages and disadvantages, and it ultimately depends on what type of project you are working on. For gaming, C++ is often preferred because of its speed and control, but C# has become increasingly popular in game development as well. It's worth trying both and seeing which one you prefer working with.
  • #281
pbuk said:
if (userSelection == EOL) continue;
Wouldn't break instead of continue also exit the loop cleanly in your example?
 
  • Like
Likes pbuk and yungman
Technology news on Phys.org
  • #282
You put the user's input in two variables, a char and an int, so you could display it in both formats. Instead, you can use just the char variable to get both formats:
Code:
cout << "the first key was: " << userSelection
      << ", the ASCII is " << int(userSelection) << endl;
We call this "casting" the char variable to int.

It works the other way, too. If you have int ascii = 97;, then cout << char(ascii) displays a.
 
  • Like
Likes pbuk
  • #283
pbuk said:
So here we go, you can also see this working at https://repl.it/repls/RealSubduedAtom. This code is missing comments of course, you might like to add them as you work through it.
C:
#include <iostream>
using namespace std;

#define EOL '\n'

int main() {
  int ascii = 0;
  char userSelection = 0;

  while (userSelection != EOL) {
    cout << "type a key and [enter]: ";
    cin.get(userSelection);
    ascii = userSelection;

    if (userSelection == EOL) continue;

    cin.ignore(255, EOL);
    cout << "the first key was: " << userSelection
      << ", the ASCII is " << ascii << endl;
  }
  return 0;
}
Thanks for the program. I think I understand, I just want to confirm one thing.

When cin.get(userSelection), it read two characters into the keyboard buffer: first character and the '\n' as last character. The userSelection contain the first character after the cin.get() line.
Then in the line if (userSelection == EOL), This read out the next character from the keyboard buffer, which is '\n'.

Just want to confirm this.

Thanks
 
  • #284
yungman said:
Thanks for the program. I think I understand, I just want to confirm one thing.

When cin.get(userSelection), it read two characters into the keyboard buffer: first character and the '\n' as last character. The userSelection contain the first character after the cin.get() line.
No, that's not quite right. First of all the code you write does not read into a buffer, this part is done by the operating system. Also, cin is actually a line input buffer cin.get which is why you have to hit enter, rather than a keyboard buffer which reads keys straight away.

So cin.get(userSelection) waits for a character to appear in the keyboard line input buffer (which will only happen after return has been entered) and then reads one character from the buffer into userSelection which is passed by reference.

yungman said:
Then in the line if (userSelection == EOL), This read out the next character from the keyboard buffer, which is '\n'.
Does it look like this code reads anything from any input buffer?
 
  • #285
jtbell said:
Wouldn't break instead of continue also exit the loop cleanly in your example?
Yes it would, but breaking out of a while(true) loop is not a very understandable way to write code. Putting the termination condition up front makes it much easier to see when you want the loop to terminate.

jtbell said:
You put the user's input in two variables, a char and an int, so you could display it in both formats. Instead, you can use just the char variable to get both formats:
Code:
cout << "the first key was: " << userSelection
      << ", the ASCII is " << int(userSelection) << endl;
We call this "casting" the char variable to int.

It works the other way, too. If you have int ascii = 97;, then cout << char(ascii) displays a.
Yes this is the way I would normally do it but the OP used two variables so I kept to that. Note that you can also cast using (int) userSelection which the compiler treats exactly the same way; I prefer this because int(userSelection) looks like a function call. Note also that when you do ascii = userSelection the compiler is performing an implicit cast. With certain compiler settings, userSelection = ascii will report a warning, @yungman can you think why?
 
  • #286
I think I got it.

Thanks
 
  • #287
pbuk said:
So cin.get(userSelection) waits for a character to appear in the keyboard line input buffer (which will only happen after return has been entered) and then reads one character from the buffer into userSelection which is passed by reference.
And of course your cin.ignore() ensures that, if you enter more than one character before pressing 'return', the program processes only the first one and skips over the rest.

A useful exercise for yungman: "comment out" the cin.ignore(), recompile, run, and then enter a word instead of a single character and see what happens.
 
  • Like
Likes yungman
  • #288
I want to know how the keyboard buffer works when using cin, cin.get and cin.ignore.

I still want to confirm about how cin.get can make it jump out of the loop with just hitting ENTER where the rest of the character, you need to enter the character, then press ENTER. Please check what I said is correct or not.

My understanding is when cin >>character, it needs to hit ENTER before it does anything. When hitting ENTER, two characters are stored in the keyboard buffer...the character and '\n'. cin read the first character, leaving the '\n' in the keyboard buffer. If cin.get() follows, it immediate read the '\n' from the last cin and treat it as a character and move on without reading the character supposed to be entered from the keyboard.

I still don't get how C++ pick which character to read in the keyboard buffer, when is it start reading, is it only read when hitting the ENTER key?
 
  • #289
yungman said:
I want to know how the keyboard buffer works when using cin, cin.get and cin.ignore.

I still want to confirm about how cin.get can make it jump out of the loop with just hitting ENTER where the rest of the character, you need to enter the character, then press ENTER. Please check what I said is correct or not.
I am sorry, I can't really understand what you are saying. If you want to work out how it works the best thing is just to play with it.

yungman said:
My understanding is when cin >>character, it needs to hit ENTER before it does anything.
Correct, you won't see anything from cin until you hit enter.
yungman said:
When hitting ENTER, two characters are stored in the keyboard buffer...the character and '\n'.
As many characters as you have typed before hitting enter are in the buffer - it could be 0, 1 or many - as well as the \n (i.e. ENTER) character.

yungman said:
cin read the first character, leaving the '\n' in the keyboard buffer.
Only if it was there - if the first and only character was \n then the buffer is now empty.

yungman said:
If cin.get() follows, it immediate read the '\n' from the last cin and treat it as a character and move on without reading the character supposed to be entered from the keyboard.
Nowhere in either your code or mine is one cin.get() followed by another.

yungman said:
I still don't get how C++ pick which character to read in the keyboard buffer, when is it start reading, is it only read when hitting the ENTER key?
Not exactly. In order for the buffer to be read, two conditions must be satisfied:
  1. the operating system must have stored characters in the buffer; this only happens when you hit ENTER
  2. your code must try to read from the buffer; this only happens when you reach the (compiled) cin.get() statement.
Once both these conditions are satisfied, cin.get() knows that you are looking for a char type so it reads exactly one byte from the buffer. If you have only typed \n, that is what you will get and the buffer will be empty so if you immediately try to flush it your program will wait around for another character to flush. That's why I checked straight away to see if ENTER was the character received from the buffer.
 
  • #290
Somewhere near the beginning of this thread I think I recommended codecademy. I think the way codecademy leads you through exercises would be useful. Please give it a try.
 
  • Like
Likes FactChecker
  • #291
jtbell said:
And of course your cin.ignore() ensures that, if you enter more than one character before pressing 'return', the program processes only the first one and skips over the rest.

A useful exercise for yungman: "comment out" the cin.ignore(), recompile, run, and then enter a word instead of a single character and see what happens.
Yes, I wrote a program with words reading in last name and first, then print out, then as to hit ENTER to quit or any other character to do it again:
C++:
// enter multiple data in one cin
#include <iostream>
using namespace std;

int main()
{
    char LastName[21] = { '\n' }, FirstName[21] = { '\n' };
    char userSelection = 'A';
    do
    {
    cout << endl;
    cout << "Enter LastName and FirstName with a space in between, then hit ENTER "<< "\n";
    cin >> LastName >> FirstName;
    cout << "\n";
    cout << "You entered  " << LastName << " " << FirstName << endl;
    cout << "\n";
    cin.ignore(255, '\n');// clear ENTER
    cout << " Hit any character to repeat, hit ENTER to quit ";
    cout << endl;
    cin.get(userSelection);
    } while (userSelection != '\n');
    return 0;
}
 
  • #292
It seems that in order to do exactly what you want (read a newline character from an input stream, just like any other char), C++ would have to allow for "unbuffered input streams." And C++ simply doesn't provide this capability. All I/O is via abstract streams, which could be connected to your keyboard and display, or to files, or to network sockets...

At least that's how I interpret the following discussion that I found with a Google search for "c++ unbuffered console input":

https://www.daniweb.com/programming...reads/451301/why-no-standard-unbuffered-input

I think I remember reading similar things elsewhere years ago when I was more involved in C++ by teaching it.

There's at least one way to accomplish your ultimate goal, which is to allow the user to terminate an input loop by simply pressing <return>. You don't read to a single char, but to an std::string. And insted of using cin >>, you use the std::getline() function, which reads from an input stream (e.g. cin) until it encounters a newline. The chars up to (but not including) the newline go into the string, and the newline is discarded. To test whether the user simply pressed <return>, you test the string to see if it's empty.

Code:
#include <iostream>
#include <string>

int main ()
{
    std::string choice = "go";
    while (choice != "")
    {
        std::cout << "Enter a character, or <return> to exit: ";
        std::getline (std::cin, choice);
        std::cout << "You entered '" << choice << "'." << std::endl;
        if (choice != "")
        {
            std::cout << "'" << choice[0] << "'" 
              << " has the ASCII code " << int(choice[0]) << "." 
              << std::endl;
        }
    }
    return 0;
}

If the user enters more than one char before pressing <return>, this program displays all of them, but displays the ASCII code for only the first one. Exercise: modify the program so it displays the ASCII codes for all the chars that the user enters.
 
  • Like
Likes Vanadium 50
  • #293
jtbell said:
It seems that in order to do exactly what you want (read a newline character from an input stream, just like any other char), C++ would have to allow for "unbuffered input streams." And C++ simply doesn't provide this capability. All I/O is via abstract streams, which could be connected to your keyboard and display, or to files, or to network sockets...
No, there is no problem with reading a newline character from an input stream as you can see from my code in #278(!). The problem with @yungman's code was that after reading one character (which could be a newline) he was calling ignore() which was waiting around for another newline.

jtbell said:
There's at least one way to accomplish your ultimate goal, which is to allow the user to terminate an input loop by simply pressing <return>. You don't read to a single char, but to an std::string. And insted of using cin >>, you use the std::getline() function, which reads from an input stream (e.g. cin) until it encounters a newline. The chars up to (but not including) the newline go into the string, and the newline is discarded. To test whether the user simply pressed <return>, you test the string to see if it's empty.
Yes, std::getline() is generally more useful than cin >>. But actually neither of these are very useful - interactive console programs aren't really a thing in C++; if I were @yungman I'd drop it and move on.
 
  • #294
I run into problem building the solution. I got an error message
C++:
// Read and write to file
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ofstream outputFile;
    outputFile.open("demofile.txt");
    cout << "Now writing data to the demofile.txt" << endl;
    outputFile << "Bach\n";
    outputFile << "Beethoven\n";
    outputFile << "Mozart\n";
    outputFile << "Schubert\n";

    outputFile.close();
    cout << "Done " << endl;
    cout << endl;
    return 0;

}
Compile error file write.jpg


I triple checked, I type everything right straight out from the book. Please help

Thanks
 
Last edited:
  • #295
The error message says that your path name length plus the generated file name length exceeds the 250 character limit. You can reduce the length by reducing the number or lengths of the concatenated components. For example, the generated file name includes your project name, so using a shorter project name would contribute to staying within the limit.

https://stackoverflow.com/questions...-file-the-spcified-path-filename-are-too-long
 
  • Like
Likes yungman
  • #296
sysprog said:
The error message says that your path name length plus the generated file name length exceeds the 250 character limit. You can reduce the length by reducing the number or lengths of the concatenated components. For example, the generated file name includes your project name, so using a shorter project name would contribute to staying within the limit.

https://stackoverflow.com/questions...-file-the-spcified-path-filename-are-too-long
I don't understand, it is a new file as you see created by the program named "demofile.txt". It only contain 4 words( 4 names). It is less than 50 characters if you count them all including the name of the file.

thanks

EDIT: I went in and deleted 3 out of 4 names, still fail.
 
Last edited:
  • #297
yungman said:
I don't understand, it is a new file as you see created by the program named "demofile.txt". It only contain 4 words( 4 names). It is less than 50 characters if you count them all including the name of the file.

thanks
Please look at the file name in the error message ##-## it's between quotation marks, and it's about 100 characters long. The file name that the message is reporting is the VS-generated name of a debug file. The fully-qualified name includes the path name, which includes everything in the directory structure from the drive letter to the most deeply nested current subdirectory. It also includes the whole of your project name, which lengthily describes the program, instead of just being something like P002. You can also rename directories to something shorter, e.g. abbreviate Microsoft to MS, and Visual Studio to VS. The full path character string from drive letter to current subdirectory doesn't appear in the message, so I can't see all the reductions you could do to reduce its length.
 
  • Like
Likes yungman
  • #298
sysprog said:
Please look at the file name in the error message it's between quotation marks, and it's about 100 characters long.
I think I copied it correctly here:

Debug\3.29 rea.63bf1643e.tlog\3.29 read write to file ifstream ofstream open close.lastbuildstate

The full path name is probably much longer, as sysprog described.

I suspect that "3.29 read write to file ifstream ofstream open close" is the name that yungman chose for the project or something related to it. It looks like an exercise number (3.29) followed by keywords describing the program to help him find it later. "lastbuildstate" looks like it might be a suffix generated by Visual Studio for internal purposes.

I suggest that he go to wherever he entered that name, and replace it with a much shorter one, maybe something like "3.29 fstream".

I'm just guessing here, because I've never used Visual Studio.

I copied and pasted the program as shown, into a .cpp file on my iMac. My compiler (g++) compiles it successfully, and it produces the expected output.
 
  • Like
Likes yungman and sysprog
  • #299
jtbell said:
Debug\3.29 rea.63bf1643e.tlog\3.29 read write to file ifstream ofstream open close.lastbuildstate

The full path name is probably much longer, as sysprog described.
jtbell said:
I'm just guessing here, because I've never used Visual Studio.
I agree with @jtbell and @sysprog that the filename of the source code is the problem.
I copied the program verbatim and ran it through VS, and it worked as expected.
 
  • Like
Likes sysprog
  • #300
I solved the problem. It is the name of my whole project I created! I tried to make it more descriptive on the name so in the future when I read the name, I can know what the program does. This is old age for you, can't remember after a while. 3.29 doesn't mean anything to me later on. I did a lot of exercise with the first book and I don't remember what they are until I open them. So for the Gaddi's book I give descriptions...AND it's too long!

Now, I deleted the whole project, saving only the source.cpp. Create a new project called 3.29 ofstream and it compiled.

VS has strange things, I found the demofile.txt that created by the program now. Also VS is strange, I before I end up deleting the whole project, I tried to change the name of EVERY file inside, it's like every time I navigate around, I see new names that I missed the first time! It just did not work trying to rename the files, finally, I had to take out the source.cpp, then try to delete. Then I had to close all the other projects ( I had the ifstream project opened in another VS program) in order to even create the new 3.29 ofstream.

Now my other ifstream doesn't read the demofile.txt. That's another story for another day. I want to work on that first.
 
Last edited:
  • Like
Likes jtbell
  • #301
yungman said:
I solved the problem. It is the name of my whole project I created!
It's a good idea to create descriptive names for your projects, but not good to go overboard on the names. I have several hundred projects that I've created over the years, but they're short with no spaces, to remind me what the basic purpose of the program is. Instead of "3.29 read write to file ifstream ofstream open close," I would have called the project "FileOutput."
yungman said:
VS has strange things, I found the demofile.txt that created by the program now. Also VS is strange, I before I end up deleting the whole project, I tried to change the name of EVERY file inside, it's like every time I navigate around, I see new names that I missed the first time! It just did not work trying to rename the files, finally, I had to take out the source.cpp, then try to delete. Then I had to close all the other projects ( I had the ifstream project opened in another VS program) in order to even create the new 3.29 ofstream.

Now my other ifstream doesn't read the demofile.txt. That's another story for another day. I want to work on that first.
What you describe may or may not be strange. The only other C/C++ compiler I've used was Borland's product, close to 30 years ago.
If you don't provide the complete path to the file, where the file needs to be depends on whether you're running your program from the debugger or from a command prompt window.
If you're running from within the debugger, the input file or output file has to be in the same directory as the source code.
If you're running the program from a command prompt window, the input file or output file has to be in the same directory as the executable.

If you do provide the complete path to the input or output file, it doesn't matter where the file is -- the program can find the file.
 
Last edited:
  • Like
Likes sysprog and yungman
  • #302
I am actually very excited on this input and output file, this, is a real interface to the outside world rather than staying in it's little cocoon. So if I want to save and open a file anywhere in the computer I can put
C++:
{
ofstream outputFile;
outputFile.open( C:\Users\alanr\Desktop\Alan amp3\SS\OPS\MOSFET\demofile.txt);
}
in order to save demofile.txt into C:\Users\alanr\Desktop\Alan amp3\SS\OPS\MOSFET?
 
  • Like
Likes sysprog
  • #303
yungman said:
I am actually very excited on this input and output file, this, is a real interface to the outside world rather than staying in it's little cocoon. So if I want to save and open a file anywhere in the computer I can put
C++:
{
ofstream outputFile;
outputFile.open( C:\Users\alanr\Desktop\Alan amp3\SS\OPS\MOSFET\demofile.txt);
}
in order to save demofile.txt into C:\Users\alanr\Desktop\Alan amp3\SS\OPS\MOSFET?
You need double quotes around the path and filename string, and because characters preceded by a backslash (\) are escaped, you need to double them up.
Code:
outputFile.open( "C:\\Users\\alanr\\Desktop\\Alan amp3\\SS\\OPS\\MOSFET\\demofile.txt");

Or you can use a single forward slash.
Code:
outputFile.open( "C:/Users/alanr/Desktop/Alan amp3/SS/OPS/MOSFET/demofile.txt");
 
  • Like
Likes sysprog and yungman
  • #304
It doesn't work, I can't find the file even though the program said it's done. this is my program.
C++:
// Read and write to file
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ofstream outputFile;
    outputFile.open("C:\Users\alanr\Desktop\C++ exercise\Gaddis\inout files\demofile.txt");
    cout << "Now writing data to the demofile.txt" << endl;
    outputFile << "Bach\n";
    outputFile << "Beethoven\n";
    outputFile << "Mozart\n";
    outputFile << "Schubert\n";

    outputFile.close();
    cout << "Done " << endl;
    cout << endl;
    return 0;

}

I search around, I can't find the file, it's not in the 3.29 folder.
I created the inout files folder and copy the address bar and add \demofile.txt to the end.
 
  • #305
I was looking at the message after compile, I don't see where the demofile.txt saved.
Compile error file write.jpg


The only thing I see in the program is the change of color as shown. It should all be RED, but the '\a' looks different. I googled, \a is alarm or alert. But alanr is the path in my computer, not much I can do.
Program question.jpg


thanks
 
  • #306
yungman said:
It doesn't work, I can't find the file even though the program said it's done
I explained why it doesn't work in post #303. Please read more carefully the posts where members are trying to help you out.
 
Last edited:
  • Like
Likes sysprog
  • #307
Mark44 said:
I explained why it doesn't work in post #303. Please read more carefully the posts where members are trying to help you out.
Thanks, got it.

I do the same thing using ifstream and point to the folder and read back successfully.

Thanks
 
  • Like
Likes sysprog
  • #308
From post #277:
yungman said:
I want the program to read in any character from the keyboard, display it and show the ASCII number. Then loop back to ask another character. I want it to exit when I hit ENTER ( ASCII = 10)
Here's a different version of what you were trying to do, written purely in C. It uses _getche(), whose declaration is in conio.h. It gets a character from the keyboard, and echoes it to the screen. A slightly different function, _getch(), doesn't echo the character.
C:
#include <cstdio>     // for printf()
#include <conio.h>    // for _getche()
#define CR 13         // <ENTER> key

int main()
{
    int userEntry;
    do
    {
        printf("Press any key:  ");
        userEntry = _getche();

        printf("\nYou hit: ");
        if (userEntry == CR) printf("<ENTER>\n");
        else printf("%c\n", (char) userEntry);
        printf("ASCII code: %d\n", userEntry);

    } while (userEntry != CR);
}
BTW, the ASCII code for the Enter key is 13 (Carriage Return). On Windows machines, when you press <Enter>, the OS inserts two characters -- CR and LF (carriage return and line feed, ASCII codes 13 and 10, respectively). On Linux and Macs (I believe), only one of these is inserted, but I don't recall which one it is.
 
  • Like
Likes sysprog
  • #309
Mark44 said:
From post #277:
Here's a different version of what you were trying to do, written purely in C. It uses _getche(), whose declaration is in conio.h. It gets a character from the keyboard, and echoes it to the screen. A slightly different function, _getch(), doesn't echo the character.
C:
#include <cstdio>     // for printf()
#include <conio.h>    // for _getche()
#define CR 13         // <ENTER> key

int main()
{
    int userEntry;
    do
    {
        printf("Press any key:  ");
        userEntry = _getche();

        printf("\nYou hit: ");
        if (userEntry == CR) printf("<ENTER>\n");
        else printf("%c\n", (char) userEntry);
        printf("ASCII code: %d\n", userEntry);

    } while (userEntry != CR);
}
BTW, the ASCII code for the Enter key is 13 (Carriage Return). On Windows machines, when you press <Enter>, the OS inserts two characters -- CR and LF (carriage return and line feed, ASCII codes 13 and 10, respectively). On Linux and Macs (I believe), only one of these is inserted, but I don't recall which one it is.
Win uses CR and LF, while *n*x uses LF only, and assumes CR.
 
  • #310
yungman said:
I was looking at the message after compile, I don't see where the demofile.txt saved.
View attachment 267210

The only thing I see in the program is the change of color as shown. It should all be RED, but the '\a' looks different. I googled, \a is alarm or alert. But alanr is the path in my computer, not much I can do.
View attachment 267211

thanks
Your string for the filename is broken. You have two options:
1) Escape the backslashes: Since the \ has a special "escape" meaning to toggle special characters in a string (e.g. \n for a newline character) you need to have some encoding for a backslash. The encoding is \\\\ . So your filename string would be "C:\\\\Users\\\\alanr\\\\Desktop\\\\ ...". That's the ugly but for some weird reason the arguably more popular solution.
2) Use forward slashes for paths. Windows understands them perfectly, and everything else uses them anyways (see the address field of your browser). Your filename string then becomes "C:/Users/alanr/Desktop/...".

Edit: Fun fact: This forum uses a similar escape mechanism for backslashes. So I had to edit my post and write \\\\\\\\ to get \\\\ show up in my post.
 
Last edited:
  • #311
Timo said:
1) Escape the backslashes: Since the \ has a special "escape" meaning to toggle special characters in a string (e.g. \n for a newline character) you need to have some encoding for a backslash. The encoding is \\ . So your filename string would be "C:\\Users\\alanr\\Desktop\\ ...". That's the ugly but for some weird reason the arguably more popular solution.
2) Use forward slashes for paths. Windows understands them perfectly, and everything else uses them anyways (see the address field of your browser). Your filename string then becomes "C:/Users/alanr/Desktop/...".
Already stated in post #303.
 
  • #312
Sorry I did not respond on the suggestions, I am hot on the trod trying to cover the things I missed now that I got a really good book, the Gaddis books is really good particular chapter 3 contains a lot of things I need and the first book just doesn't explain. I went through chapter 1 to 3 and finishing up chapter 4 today on conditional if then else, switch . I have not started on chapter 5 and beyond, I just copy some of the stuffs from the notes on the first book. I hope to breeze through chapter 5 as it's on loops. Chapter 4 and 5 are easy, I am looking forward to chapter 6 on FUNCTIONS, calling a function and all. I am excited getting into the modular programming.

I am sure I'll be back here with questions soon, but for now I just want to bull through the stuffs I learned from the first book and complete the holes not covered.

Thanks
 

Attachments

  • Gaddis notes.docx
    37.1 KB · Views: 129
  • #313
Mark44 said:
Already stated in post #303.
:oldbiggrin:. I actually introduced a bug into our software yesterday that was totally avoidable if I had read the comment around line 4000 (of 27k) in the file I was editing.
 
  • Like
Likes Mark44
  • #314
I don't get the feel with this:

I have a question about setw() and setprecision() for cin. I know for character string, I can setw to limit the maximum character it will read in eg.
cin >> setw(4) >> input; If I type in "Alan", input only store "Ala" and '\n'.

How about numbers? say cin >> setw(2) >> number; I don't see there is a limit, I can put 12345, it will take the whole 12345.

1) My question is whether cin >> setw() work on numbers? How do I limit say number <=1000? Is there even a way? I tried setprecision(), it won't do it.

2) Is setw() more for lining up the numbers to the right on display? eg.
cout << "(" << setw(6) << number << ")"; will give (____12) if number = 12.(I have to use____for empty space).

This is the test program I am playing with
C++:
// setw() setprecision() and fixed do while
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

    double doubleValue = 91.0;
    cout << "Enter a double number   "; cin >> setw(2)>> doubleValue; cout << endl; cout << endl;
    cout << "(" << setw(6)<< setprecision(4) << doubleValue << ")" << endl; cout << endl;
    return 0;
}

Thanks
 
  • #315
yungman said:
1) My question is whether cin >> setw() work on numbers? How do I limit say number <=1000? Is there even a way? I tried setprecision(), it won't do it.
Although the setw() and setprecision() manipulators work with both output streams and input streams, they are most often used to format output streams. This VS documentation -- https://docs.microsoft.com/en-us/cpp/standard-library/input-stream-manipulators?view=vs-2019 -- (with emphasis added) says this.
Many manipulators, such as setprecision, are defined for the ios class and thus apply to input streams. Few manipulators, however, actually affect input stream objects. Of those that do, the most important are the radix manipulators, dec, oct, and hex, which determine the conversion base used with numbers from the input stream.
yungman said:
2) Is setw() more for lining up the numbers to the right on display? eg.
cout << "(" << setw(6) << number << ")"; will give (____12) if number = 12.(I have to use____for empty space).
setw() specifies the width of the display field for the next element in the stream. The ios_base class has flags that can be used to left-justify the thing being displayed, or right-justify it, convert lowercase characters to uppercase, display the thing in decimal, hex, or octal, and some others
 
  • Like
Likes yungman
Back
Top