Question: Need this C++ program converted to C. Thanks #incl... Bookmark Need th
ID: 3846337 • Letter: Q
Question
Question: Need this C++ program converted to C. Thanks #incl...
Bookmark
Need this C++ program converted to C. Thanks
#include <iostream>
#include <string>
#include <vector>
//FIXME: stringstream library
//FIXME: stream manipulation library
using namespace std;
int main() {
/* Type code here. */
string title, heading1, heading2, s;
vector<string> data;
vector<int> point;
cout<<"Enter a title for the data: ";
getline(cin, title);
cout<<"You entered: "<<title<<endl;
cout<<"Enter the column 1 header: ";
getline(cin, heading1);
cout<<"You entered: "<<heading1<<endl;
cout<<"Enter the column 2 header: ";
getline(cin, heading2);
cout<<"You entered: "<<heading2<<endl;
while(1){
cout<<"Enter a data point (-1 to stop input): ";
getline(cin, s);
if(s == "-1"){
break;
}
if(s.find(',') == -1){
cout<<"Error: No comma in string."<<endl;
}
else{
int commaCount = 0;
for(int i=0; i<s.length(); i++){
if(s.at(i) == ','){
commaCount++;
if(commaCount > 1){
break;
}
}
}
if(commaCount == 1){
data.push_back (s.substr(0, s.find(',')));
point.push_back (stoi(s.substr(s.find(',')+1, s.length()-1)));
cout<<"Data string: "<<s.substr(0, s.find(','))<<endl;
cout<<"Data integer: "<<stoi(s.substr(s.find(',')+1, s.length()-1))<<endl;
}
else{
cout<<"Error: Too many commas in input."<<endl;
}
}
}
cout<<" "<<title<<" "<<endl;
cout<<heading1<<" | "<<heading2<<endl;
for(int i=0; i<data.size(); i++){
cout<<data[i]<<" | "<<point[i]<<endl;
}
for(int i=0; i<data.size(); i++){
cout<<data[i]<<" ";
for(int j=0; j<point[i]; j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}
Explanation / Answer
As asked in question, above program has been converted into C language :
-- Strings are replaced by Array of characters.
-- Iostream.h s replaced by stdio.h
-- cout is replaced by printf statement
-- cin is replaced by scanf statement
-- Vectors are replaced by Array
#include <stdio.h> // Include stdio.h as iostream is not supported by C
#include <string.h> // added .h otherwise C compiler won't recognize
//#include <vector.h> // Vector cannot be used in C
//In C we use ARRAY instead of vector for storing data
//FIXME: charstream library
//FIXME: stream manipulation library
//using namespace std; -- Does not require in C language
int main() {
/* Type code here. */
char title[10], heading1[10], heading2[10], s[10]; // String data type is replaced by char of array
char data[10]; //vector<char> data; // String data type is replaced by char of array
char point[10]; //vector<int> point; // String data type is replaced by char of array
printf("Enter a title for the data: "); // printf( replaced by printf in C
scanf("%s",title); // scanf( replaced by scanf in C for input
printf("You entered: %s" ,title);
printf("Enter the column 1 header: ");
scanf("%s",heading1);
printf("You entered: %s",heading1);
printf("Enter the column 2 header: ");
scanf("%s",heading2);
printf("You entered: %s",heading2);
while(1){
printf("Enter a data point (-1 to stop input): ");
scanf("%s",s);
if(s == ''){
break;
}
if(s.find(',') == -1){
printf("Error: No comma in char.");
}
else{
int commaCount = 0;
for(int i=0; i<s.length(); i++){
if(s.at(i) == ','){
commaCount++;
if(commaCount > 1){
break;
}
}
}
if(commaCount == 1){
data.push_back (s.substr(0, s.find(',')));
point.push_back (stoi(s.substr(s.find(',')+1, s.length()-1)));
printf("Data char:%s ",s.substr(0, s.find(',')));
printf("Data integer:%s ",stoi(s.substr(s.find(',')+1, s.length()-1)));
}
else{
printf("Error: Too many commas in input.");
}
}
}
printf(" %s",$title" ");
printf("%s",&heading1" | %s"&heading2);
for(int i=0; i<data.size(); i++){
printf("%s",data[i]" | %s",point[i]);
}
for(int i=0; i<data.size(); i++){
printf("%s",data[i]," ");
for(int j=0; j<point[i]; j++){
printf("*");
}
printf(' ');
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.