PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label cin. Show all posts
Showing posts with label cin. Show all posts

Saturday, October 29, 2022

[FIXED] How to signify no more input for string ss in the loop while (cin >> ss)

 October 29, 2022     c++, cin, eof, file-io     No comments   

Issue

I used "cin" to read words from input stream, which like

int main( ){
     string word;
     while (cin >> word){
         //do sth on the input word
     }

    // perform some other operations
}

The code structure is something like the above one. It is compilable. During the execution, I keep inputting something like

aa bb cc dd

My question is how to end this input? In other words, suppose the textfile is just "aa bb cc dd". But I do not know how to let the program know that the file ends.


Solution

Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as CTRL-D.

This EOF character isn't needed when you are reading in a file. This is because once you hit the end of your input stream, there is nothing left to "cin"(because the stream is now closed), thus the while loop exits.



Answered By - Mike Lewis
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, August 18, 2022

[FIXED] How can i get an input of 3 double variables with cin in C++?

 August 18, 2022     c++, cin, double, input, output     No comments   

Issue

I must take 3 double variables as an input and find their mean. If i input a integer as a input(for example 5) program works. but if i input a fractional number(for example 5.3) it wont take the other 2 inputs and close.

here is my code:

#include <iostream>
using namespace std;
int main()
{
   double y1,y2,y3,ort; 
   cout<<"1. input : \n";
   cin>>y1;
   cout<<"2. input : \n"; 
   cin>>y2;
   cout<<"3. input : \n";
   cin>>y3;
   ort=(y1+y2+y3)/3;
   cout<<"Value : "<< ort << "\n" ; 
   system("pause");
   return 0;
}

Solution

Your program works as expected. At least the mean is computed accordingly, the pause call is not working for me.

1. input : 
1.3
2. input : 
2.3
3. input : 
3.3
Value : 2.3
sh: 1: pause: not found
Press <RETURN> to close this window...

Maybe you have wrong locale set.

You can try add:

#include <locale.h> 
setlocale(LC_ALL,"C")

As I just read your comment if you input 3 its working. but if you input 3,2 its not working., floating points are by programming standards separated with ., not ,. E.g. you have to write 3.2. That is what English speaking countries have also in their standard.



Answered By - Jan Hackenberg
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How is "cin>>" different from "cin.get"? OR Is it not?

 August 18, 2022     c++, cin, output     No comments   

Issue

I was coding a program for Multiplication Tables... And I used a=cin.get(); and cin>>a; to input the value. I got different results with each. The code is:

#include"pch.h"
#include<iostream>

using namespace std;

int main()

{

float a, b;

cout << "Enter the number:";
a = cin.get();
cout << "The Table for the given number is:" << endl;
for (b = 1; b <= 10; ++b)
{
    cout << a << " * " << b << " = " << a * b << endl;
}

return 0;
}

And the other one is:

#include"pch.h"
#include<iostream>

using namespace std; 

int main()

{  

float a, b;

cout << "Enter the number:";
cin >> a;
cout << "The Table for the given number is:" << endl;
for (b = 1; b <= 10; ++b)
{
    cout << a << " * " << b << " = " << a * b << endl;
}

return 0;
}

The one with cin>>a; worked fine. I once read that cin.get() is also used to get the value of the variable. Does it have some other use instead of this?

Thanks,


Solution

Like many newbies you're a bit confused about types. cin >> a will read from cin into variable a no matter what type a is, so float, int, std::string etc all work with >>. That's a simplification but close enough for now.

a = cin.get() is for reading single characters only, it returns the next character in the input. What's happening in your first program is that a char value from get() is being converted to a float value. Skipping over the details but that's not something that makes a lot of sense, which is why you get the strange results.

Another difference between >> and get() is that >> will skip whitespace but get() will not. So if you want to read a single character irrespective of whether it's whitespace or not then use get() otherwise use >>.



Answered By - john
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing