7. char* filename; //will reprompt the user for a filename until he enters a val
ID: 3677474 • Letter: 7
Question
7.
char* filename;
//will reprompt the user for a filename until he enters a valid file name and we break the while loop in if
while(true)
{
cout<<"Enter a input file name"<
cin>>filename;
if (std::ifstream(name))
{
break;
}
}
8.
output:
+2
+2
9.
string s;
cout<<"enter a phrase"<
cin>>s;
int countSpaces=0;
for(int i=0;i
{
if(s[i]==' ')
{
countSpaces++;
}
}
cout<<"number of spaces in the phrase are"<
10.
int a,b,sum;
cout<<"enter two numbers"<
cin>>a>>b;
sum=a+b;
ofstream file(name);
file<<"First number "<
file<<"Second number "<
file<<"their sum "<
file.close();
Q7- Write a code segment that asks the user for an input file name, and allows the user to re-enter the file name if the file doesn’t exists.
Q8- What output will be produced when the following lines are executed? (Be precise!) cout.width (5);
cout << “+” << 2 < cout <
Q9- Read a phrase from the keyboard and display how many spaces are in the phrase.
Q10- Write a code segment that prompts for and reads in two whole numbers from the keyboard and writes both numbers and their sum into an output file called ‘Q10.txt’. The program should write to the end of ‘Q10.txt’ if the file already exists.
----------
im expecting different answers then the answers below:
7.
char* filename;
//will reprompt the user for a filename until he enters a valid file name and we break the while loop in if
while(true)
{
cout<<"Enter a input file name"<<endl;
cin>>filename;
if (std::ifstream(name))
{
break;
}
}
8.
output:
+2
+2
9.
string s;
cout<<"enter a phrase"<<endl;
cin>>s;
int countSpaces=0;
for(int i=0;i<s.length();++i)
{
if(s[i]==' ')
{
countSpaces++;
}
}
cout<<"number of spaces in the phrase are"<<countSpaces;
10.
int a,b,sum;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
sum=a+b;
ofstream file(name);
file<<"First number "<<a;
file<<"Second number "<<b;
file<<"their sum "<<sum;
file.close();
Explanation / Answer
7.
ifstream infile;
8.
The output of the code
cout.width (5);
cout << “+” << 2 < cout < is +2.
9.
#include<iostream.h>
void main()
{
string s;
cout<<"enter a phrase"<<endl;
cin>>s;
int countSpaces=0;
for(int i=0;i<s.length();++i)
{
if(s[i]==' ')
{
countSpaces++;
}
}
cout<<"number of spaces in the phrase are"<<countSpaces;
}
10.
int a,b,sum;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
sum=a+b;
ofstream file(name);
file<<"First number "<<a;
file<<"Second number "<<b;
file<<"their sum "<<sum;
cin.getline(sum);// write inputted data into the file.
file.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.