why cout not works before getche() in VC++ 6.0? - visual-c++

I want to show a message before an input :
cout<<"Enter a char:";
ch = getche();
But when running program it does not show the message and getche() works and after that the message is showed !!!
The whole program :
#include <iostream.h>
#include <conio.h>
int main(){
int ch;
cout<<"Enter a char:";
//cin.ignore();
ch = getche();
return 0;
}
The whole problem that, i want to do is this:
1- show a message
2- enter ONLY one char
3- do something immediately after entering the char

Adding endl soleved the problem :
cout<<"Enter a char:"<<endl;
ch = getche();

Related

Why doesn't string class take in two words separated with space?

I want to take in persons name using string object. But in my code if I put two part name separated with a space, only first part is displayed. My understanding is .c_str() returns a pointer to stored string with terminal null. Why is there a problem with space. I'm new to C++ and using Code::Blocks 13.12. This is a simplified version of the problem that I have in another program that I wrote.
Thanks in advance.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
string sCusName;
cout << "Please enter your name-> ";
cin >> sCusName;
int xsize = sCusName.length();
char *tempBuffer = new char[xsize+1];
strncpy(tempBuffer, sCusName.c_str(),xsize+1);
cout << tempBuffer << " is a beautiful name." << endl;
return 0;
}
When I enter single part name, program works fine. But if I put in two part name separated with space. Only first part is taken in.
it is not possible to read multi-word string using cin rather you should use getline() the getline() function takes two arguments cin and string variable forexample:
int main()
{
string sCusName;
cout << "Please enter your name-> ";
getline(cin,sCusName);
cout << sCusName << " is a beautiful name." << endl;
return 0;
}

Convert string with 6 digits before decimal to Double

I have a string with the value 788597.31 and I am converting this value to double but when I print the variable only 788597 is displayed. I have used std::stod(string) and even stringstream but everytime I get the same previous value. Can anybody help me with this?
I want to store this string value in a double varaible.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s="788597.31";
double d=stod(s);
cout<<d<<" ";
stringstream g;
double a;
g<<s; g>>a;
cout<<a;
return 0;
}
The problem is in how you are printing your result, not in the string parsing. This program:
#include <iostream>
using namespace std;
int main() {
cout << 788597.31 << endl;
return 0;
}
also prints 788597.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setprecision(10) << 788597.31 << endl;
return 0;
}
prints 788597.31
If you want to see more than the default 6 significant digits your program needs to say so.

How to scan strings with spaces in cpp in a new line?

enter image description here3(no of cases)
hello world
a b c d
data structures and algorithms.
Let say above is the given input format.Each string starts in a new line.Can i know how to read in the above way in cpp.I tried with getline function.It didn't work.
The getline method should work just fine. The following code works fine.
#include <iostream>
using namespace std;
int main(){
string s[3];
getline(cin, s[0]);
getline(cin, s[1]);
getline(cin, s[2]);
cout << s[0] << endl << s[1] << endl << s[2];
return 0;
}
EDIT:
After your clarification, it seems that the problem is actually that the buffer is not being cleared properly. So just use cin.ignore(); after cin >> t;
Refer to the code below:
#include <iostream>
using namespace std;
int main(){
int t;
cin >>t;
cin.ignore(); // This should clear the input buffer
string s;
getline(cin, s);
cout << s << endl;
return 0;
}

c++ extracting command line arguments

Visual Studio 2013 C++. what I am trying to do is, I will ask for a string and extract each letter from that string. (I know it is already char) but I dont know how to extract each letter, and find out number of chars entered. I do not know how to stop the command line window. getchar() only gets one char but the user may enter several chars continuously, like a string. Any idea? Thanks.
int main(int argc, char *argv[]) {
string firstarg;
if (argc < 1)
{
//Check see if str passed
cout << "No string entered....";
}
else
{
firstarg = argv[1];
cout << firstarg;
}
}

How to find a character string within a string?

It's only every once in awhile that I have to write in c++. My problem is how do you find a character string within a string? Then save that line, and then look for a set of numbers in that line. For example I have a text file that looks like this.
Address Length Name
87623498 2 dog
12345678 4 cat
98737289 1 bird
I want to search for "cat" and then store the numbers associated with it (12345678) and (4) to different variable names. Here is some code I have written, but it's not close to being correct. I have an .exe that calls this DLL. Any help is appreciated!
`
#include
#include
#include
#include
#include
char* File_Path; //Path is sent by another program
char* Name; //value is being sent by another program
unsigned long Address;
int Length;
int found = 0;
{
using namespace std;
std::string Line;
std::ifstream infile;
infile.open (File_Path);
if (infile.is_open())
{
std::ofstream outfile;
outfile.open ("C:\\debug\\test.txt")
while (infile.good() && found == 0)
{
std::getline(infile,Line);
//if (Name is within Line){
/*I want to say IF char* Name = a part of the string line
ie. Name = cat and the string Line is (12345678 4 cat)
Then store the number (12345678 = Address), and
(4 = Length) I hope that makes sense :/
*/
outfile << Line; //output the stored line for debug
outfile << address; //output the stored address for debug
outfile << length; //output the stored length for debug
found = 1;
}
outfile.close();
infile.close();
}
return 0;
}
`
Please help! Thank you!

Resources